json 全称:javaScript Object Notation,是一种轻量级的数据交换格式,json最广泛应用在AJAX中web服务器和客户端的通讯格式,现在也常用于https的请求中。
json的四种使用方法:
有S结尾的是处理字符串的 ,没有的是处理文件的。
json的转换:将jason转换为其他格式
(1)json.loads 字符串格式
 
aa = dict(name='liuq',sex='man',age=29)
cc = json.dumps(aa)
dd = json.loads(cc)
print (dd)
print (type(dd))
print (dd['name'])
C:\Python27\python.exe E:/untitled/moudule/Jason.py
{u'age': 29, u'name': u'liuq', u'sex': u'man'}
<type 'dict'>
liuq

Process finished with exit code 0

 

(2)json.load 文件格式
 
str1 = '{"a":"aaa","b":"bbb","c":"ccc"}'
print (type(str1))
with open('2.txt','w')as f:
    json.dump(str1,f)
with open('2.txt','r')as f1:
    dat=json.load(f1)
    print (dat)
    print (type(dat))
C:\Python27\python.exe E:/untitled/moudule/Jason.py
<type 'str'>
{"a":"aaa","b":"bbb","c":"ccc"}
<type 'unicode'>

Process finished with exit code 0
jason的加载:将其他格式的文件转换为jason
(3)json.dumps字符串格式
import json
aa = dict(name='liuq',sex='man',age=29)
print (aa)
print (type(aa))
cc = json.dumps(aa)
print (cc)
print (type(cc))
C:\Python27\python.exe E:/untitled/moudule/Jason.py
{'age': 29, 'name': 'liuq', 'sex': 'man'}
<type 'dict'>
{"age": 29, "name": "liuq", "sex": "man"}
<type 'str'>

Process finished with exit code 0

 

 
得出得字符串需要在 json.cn中进行解析:
 
 
(4)json.dump文件格式
 
str1 = '{"a":"aaa","b":"bbb","c":"ccc"}'
print (type(str1))
with open('2.txt','w')as f:
    json.dump(str1,f)
"{\"a\":\"aaa\",\"b\":\"bbb\",\"c\":\"ccc\"}"