Simplejson 是一個可以將字符或者文件解析成json的python庫,能夠將python的字典和列表解析成一個json字符.python
咱們指定下面的字符爲json的數據c++
json_string='{"姓":"wen","名":"昌壽"}'json
咱們經過下面的方式進行解析spa
import simplejson as json
parsed_json=json.loads(json_string)code
經過上面的操做咱們能夠經過普通的字典的方式進行訪問對象
print(parsed_json['姓名'])
"wen"unicode
你也能夠經過下面的方式將一個dict轉換成json對象string
d={'first_name':'wen',it
'second_name':'changshou',table
title':['python','c++'],
}
print(json.dumps(d))
{"title": ["python", "c++"], "first_name": "wen", "second_name": "changshou"}
JSON | Python 2 | Python 3 |
---|---|---|
object | dict | dict |
array | list | list |
string | unicode | str |
number (int) | int, long | int |
number (real) | float | float |
true | True | True |
false | False | False |
null |