Python基础-处理json函数

#json是一种通用的数据类型,所有的语言都认识
#json是一个字符串,json串里面都得是双引号,主要是这四个函数
#dump
#dumps
#load
#loads

import json
names = {
"name":"王俊熹",
"age":36,
"assfdf":223,
"sdfsdf":23232323,
"sdfsdf1":23232323
}
# f = open('wjx.jmy','w',encoding='utf-8')
# res = json.dumps(names,ensure_ascii=False) #把字典转成json串
# names_json='{"assfdf": 223, "age": 36, "sdfsdf1": 23232323, "name": "王俊熹", "sdfsdf": 23232323}'
# names = json.loads(names_json)
# print(type(names))
# print(names)
# print(type(res))
# f.write(res)
res= open('wjx.json',encoding='utf-8')
name_dic = json.load(res) #传一个文件对象,自动转成字典,他会自己读文件,不用咱们再读一次
# print(type(name_dic))
# print(name_dic)
# fw=open('zdd.json','w',encoding='utf-8')
# json.dump(names,fw,ensure_ascii=False,indent=10)#写文件里面的时候,indent几个缩进

print(json.dumps(names,indent=10))
原文地址:https://www.cnblogs.com/niuniu2018/p/7806522.html