目录
  • json支持的格式:
    • 代码操作
      • 1.json转化为python
      • 2. python序列化为json
  • 总结

    json转化为python表示反序列化

    python转化为json表示为序列化

    json是python的内置模块,不需要安装

    json支持的格式:

    json python
    对象(object) 字典(dict)
    数组(array) 列表(list)
    字符串(string) 字符串(str)
    整数(int) 整数(int)
    实数(float) 实数(float)
    true true
    false false
    null none

    代码操作

    1.json转化为python

    json提供的两个函数支持json字符串反序列化为一个python对象

    json.loads(s)
    其中s表示字符串

    import json
    
    str_json = '{"name":"张三","age":24}'
    res = json.loads(str_json)
    print(res, type(res))  #{'name': '张三', 'age': 24} <class 'dict'>
    

    json.load(fb)
    fb:表示为文件对象test.json

    test.json

    {
      "name":"张三",
      "age":24,
      "friends": [{
        "name": "李四",
        "age": 23
      },
        {
          "name": "王麻子",
          "age": 24
        }],
      "hobby": ["玩游戏","看电影"]
    }
    
    import json
    
    with open('test.json', 'r', encoding='utf-8') as f:
        res = json.load(f)
        print(res, type(res))
      # {'name': '张三', 'age': 24, 'friends': [{'name': '李四', 'age': 23}, {'name': '王麻子', 'age': 24}], 'hobby': ['玩游戏', '看电影']} <class 'dict'>
    

    json字符串一般不会单独出现

    json.loads('"test"')  #'test'
    

    一般会放在字典或者列表中

    json.loads('["test1","test2","test3"]')  #['test1','test2','test3']
    

    2. python序列化为json

    json.dumps(obj,ensure_ascii=true,indent=none,sort_keys=false)

    • obj:表示为python 对象
    • ensure_ascii: 默认为 true,输出保证将所有输入的非 ascii 字符转义。如果 ensure_ascii 是 false,这些字符会原样输出。
    • indent:一个非负整数或者字符串,json 数组元素和对象成员会被美化输出为该值指定的缩进等级。如果缩进等级为零、负数或者 “”,则只会添加换行符。none(默认值)选择最紧凑的表达。使用一个正整数会让每一层缩进同样数量的空格。如果 indent 是一个字符串(比如 “\t”),那个字符串会被用于缩进每一层。
    • sort_keys:为 true(more 为 false),表示字典的输出会以键的顺序排序。
    import json
    
    data = {
      "name":"张三",
      "age":24,
      "friends": [{
        "name": "李四",
        "age": 23
      },
        {
          "name": "王麻子",
          "age": 24
        }],
      "hobby": ["玩游戏","看电影"]
    }
    res = json.dumps(data,ensure_ascii=false,indent=2, )
    print(res, type(res))
    

    打印的结果为

    {
      “name”: “张三”,
      “age”: 24,
      “friends”: [
        {
          “name”: “李四”,
          “age”: 23
        },
        {
          “name”: “王麻子”,
          “age”: 24
        }
      ],
      “hobby”: [
        “玩游戏”,
        “看电影”
      ]
    } <class ‘str’>

    json.dump(obj,fb,ensure_ascii=true,indent=none,sort_keys=false)

    • obj:表示为python对象
    • fb:表示为文本写打开的文件对象
    import json
    
    data = {
      "name":"张三",
      "age":24,
      "friends": [{
        "name": "李四",
        "age": 23
      },
        {
          "name": "王麻子",
          "age": 24
        }],
      "hobby": ["玩游戏","看电影"]
    }
    with open('test1.json', 'w', encoding='utf-8') as f:
        json.dump(data, fp=f, ensure_ascii=false, indent=2)
    

    写入的结果为

    test1.json

    {
      "name": "张三",
      "age": 24,
      "friends": [
        {
          "name": "李四",
          "age": 23
        },
        {
          "name": "王麻子",
          "age": 24
        }
      ],
      "hobby": [
        "玩游戏",
        "看电影"
      ]
    }
    

    总结

    本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注www.887551.com的更多内容!