时间:2021-07-01 10:21:17 帮助过:58人阅读
下面的代码片段是在Python中解析JSON的例子
import json json_input = '{ "one": 1, "two": { "list": [ {"item":"A"},{"item":"B"} ] } }' try: decoded = json.loads(json_input) # pretty printing of json-formatted string print json.dumps(decoded, sort_keys=True, indent=4) print "JSON parsing example: ", decoded['one'] print "Complex JSON parsing example: ", decoded['two']['list'][1]['item'] except (ValueError, KeyError, TypeError): print "JSON format error"
下面是例子打印的结果
{ "one": 1, "two": { "list": [ { "item": "A" }, { "item": "B" } ] } } JSON parsing example: 1 Complex JSON parsing example: B