下面的代码片段是在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
查看更多关于在python中转换JSON字符串的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did82929