一 Json
http://json.org/
json文法:
1) string:value 可以看做是json的最小單位,json由string:value的集合組成,其中value中可以嵌套string:value;
2) string為雙引號包圍的unicode字串,特殊符號需要\來轉義;
3) value可以為string:value,array,string,number,true,false,null;
一個執行個體:
{ "firstName":"John", "lastName" :"Smith", "adress":{ "streetAddress":"21 2nd Street", "city":"New York", "state":"NY", "postalCode":10021 }, "phoneNumbers":[ "212 555-1234", "646 555-4567" ]
}
二 python 與 json
三 python的json模組
import json
testStr = '''
{
"firstName":"John",
"lastName" :"Smith",
"adress":{
"streetAddress":"21 2nd Street",
"city":"New York",
"state":"NY",
"postalCode":10021
},
"phoneNumbers":[
"212 555-1234",
"646 555-4567"
]
}
'''
# Deserialize s (a str or unicode instance containing a JSON document) to a Python object.
obj = json.loads(testStr)
print("firstName:" + obj["firstName"])
print("city:" + obj["adress"]["city"])
print("phoneNumbers:" + str(obj["phoneNumbers"]))
# Serialize obj to a JSON formatted str.
str = json.dumps(obj,indent=2)
print(str)
the output:
firstName:John
city:New York
phoneNumbers:[u'212 555-1234', u'646 555-4567']
{
"lastName": "Smith",
"phoneNumbers": [
"212 555-1234",
"646 555-4567"
],
"adress": {
"postalCode": 10021,
"city": "New York",
"streetAddress": "21 2nd Street",
"state": "NY"
},
"firstName": "John"
}
完!