__author__ = "JentZhang" import json user_info = {"id": 1000, "name": "zhangsan", "age": 25, "address": "xxxxxxxxxx", "mobile": ""} # 将字典转换为JSON字符串
json_str = json.dumps(user_info)
print(json_str)
print(type(json_str)) # 将JSON字符串转换为字典
str_json = '{"name":"zhangsan","age":20}'
json_obj = json.loads(str_json)
print(json_obj)
print(type(json_obj)) f = open('json_file','w')
dic = {'k1':'v1','k2':'v2','k3':'v3'}
json.dump(dic,f) #dump方法接收一个文件句柄,直接将字典转换成json字符串写入文件
f.close() f = open('json_file')
dic2 = json.load(f) #load方法接收一个文件句柄,直接将文件中的json字符串转换成数据结构返回
f.close()
print(type(dic2),dic2)