python中dict和json区别

1.json 和 字典 区别

>>>import json

>>>json.dumps({1:2})

>>>'{"1":2}'

--------------------

>>>{1:2}

>>>{1:@}

其中字典的格式是字典,json的格式是字符串,在传输的时候用的是字符串,所以如果要传输字典内容,就需要先进行字典转json。


json中必须使用双引号,dict则可以用单引号也可以用双引号

---------------------------------------------------

2.json.dump()/json.load()  和  json.dumps()/json.loads() 区别

json.dumps()/json.loads()用来编码和解码json字符串数据

json.dump()/json.load()用来处理文件

eg:

import json
json_content = {'a':'1111','b':'2222','c':'3333','d':'4444'}
with open('json_file.json','w') as f:
    json.dump(json_content, f)
with open('json_file.json', 'r') as f:
    content = json.load(f)
    print(content)



猜你喜欢

转载自blog.csdn.net/qq_32446743/article/details/80060220