json

1、python中的数据类型和json中的对应关系。
   +-------------------+---------------+
    | Python                 | JSON              |
   +===========+=========+
    | dict                       | objects'{"":""}' |
    +-------------------+---------------+
    | list, tuple              |   array[]           |
    +-------------------+---------------+
    | str                         |      string  '""' |
    +-------------------+---------------+
    | int, float                | 'number'        |
    +-------------------+---------------+
    | True                      | 'true '            |
    +-------------------+---------------+
    | False                     | 'false'             |
    +-------------------+---------------+
    | None                    |  'null'              |
    +-------------------+---------------+
注意:对应的objects'{"":""}'数据格式中,要想转化成python中的dict,其中的键值对必须是双引号括起来的(如此才符合json数据格式的标准规定,才能是一种json数据格式)。
 
2、python数据格式和json相互转化的对应方法
python---->json    json.dumps()
json---->python    json.loads
 
例如:
print(json.loads("true")) ===>True
 
3、写到本地操作
import json
info={"name":'alex'}

#写到本地操作:
with open("json.txt","w") as f:
    f.write(json.dumps(info))

# 读取操作
with open("json.txt",'r') as f:
    data=f.read()

data=json.loads(data)
print(data)
 
 
 
原文地址:https://www.cnblogs.com/aberwang/p/10397513.html