json 模块
json
是Python
自带的模块,无需安装。
示例:
#!/usr/bin/env python
# coding: utf-8
import json
json_str = '{"result":{"count":{"total": 2110}}}'
json_list = json.loads(json_str)
total = json_list['result']['count']['total']
$ python
Python 2.7.10 (default, Jul 15 2017, 17:16:57)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import json
>>>
>>> jstr = '{"result":{"count":{"total":2110}}}'
>>> jlist = json.loads(jstr)
>>> jlist
{u'result': {u'count': {u'total': 2110}}}
>>> total = jlist['result']['count']['total']
>>> total
2110
>>>
# json encode
jstr = json.dump(some_data)
# json decode
data = json.loads(json_string)