有没有关于在python中解析json属性的好教程?我希望能够解析“ok”字段的真实值。以及名为“client_ind_1”的索引。我不明白这个主题的python文档报道。如果有人可以向我解释或指出更好的资源,那就太棒了。
我的json字符串如下所示:
{
"ok": true,
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"indices": {
"client_ind_2": {
"index": {
"primary_size": "2.5mb",
"primary_size_in_bytes": 2710326,
"size": "2.5mb",
"size_in_bytes": 2710326
}
}
}
}
提前谢谢。
答案 0 :(得分:17)
import json
a = """{
"ok": true,
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"indices": {
"client_ind_2": {
"index": {
"primary_size": "2.5mb",
"primary_size_in_bytes": 2710326,
"size": "2.5mb",
"size_in_bytes": 2710326
}
}
}
}"""
b = json.loads(a)
print(b['ok'])
print(b['indices']['client_ind_2']['index'])
这会将json作为python字典,并打印出'ok'和你想要的索引值:
True
{u'primary_size': u'2.5mb', u'primary_size_in_bytes': 2710326, u'size_in_bytes': 2710326, u'size': u'2.5mb'}
答案 1 :(得分:1)
import json
dct = json.loads(my_json_str)
is_ok = dct['ok']
client_index = dct['indices']['client_ind_2']['index']