Json的编写
Json
格式:
1. 对象以{开始 }结束
2. 每个对象包含 名称:值
3. 多个值用[ ]包含
4. 值的形式为字符串,数字和布尔值
View Code
{ "firstName": "John", "lastName": "Smith", "male": true, "age": 25, "address": { "streetAddress": "21 2nd Street", "city": "New York", "state": "NY", "postalCode": "10021" }, "phoneNumber": [ { "type": "home", "number": "212 555-1234" }, { "type": "fax", "number": "646 555-4567" } ] }
用Python编写:
1. 用列表和字典形式
2. 写:
json.dumps(data1【值】,indent=4【表示缩进4个空格】,sort_keys=True【key排序】)
3. 读:
dd1=json.loads(content)
4: 代码
practice json
import json data1 = [{'b':789,'c':\ {"lat":"39.990","lng":"116.397"},'a':123}] d1=json.dumps(data1,indent=4,sort_keys=True) f=open('/home/syu/Downloads/Vieri.json') content=f.read() dd1=json.loads(content) print d1 for item in dd1: for file in item: print file,":",item[file]