JSON数据提取

发布时间:2019-03-08 20:31:53编辑:auto阅读(2013)

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,它使得人们很容易的进行阅读和编写。同时也方便了机器进行解析和生成。JSON在数据交换中起到了一个载体的作用,承载着相互传递的数据。JSON适用于进行数据交互的场景,比如网站前台与后台之间的数据交互。

     json模块是Python自带的模块,Python3 中可以使用 json 模块来对 JSON 数据进行编解码:

    • json.dumps(): 对数据进行编码。
    • json.loads(): 对数据进行解码。

     

     

    Python 数据类型编码为 JSON数据类型:

    Python JSON
    dict object
    list, tuple array
    str string
    int, float, int- & float-derived Enums number
    True true
    False false
    None null

    JSON数据类型 解码为 Python 数据类型:

    JSON Python
    object dict
    array list
    string str
    number (int) int
    number (real) float
    true True
    false False
    null None

    ipython3交互环境测试代码:

     

    with open("test.json", 'w', encoding='utf8') as f:
        json.dump(dic, f)

    with open("test.json", 'w', encoding='utf8') as f:
        json.dump(dic, f, ensure_ascii=False, indent=2) 

     

关键字