在python中解析json字段 [英] parsing json fields in python

查看:455
本文介绍了在python中解析json字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于在python中解析json属性是否有很好的教程?我希望能够解析确定"字段的真实值.以及名为"client_ind_1"的索引.我不了解有关此主题的python文档.如果有人可以向我解释或指出更好的资源,那就太好了.

Is there a good tutorial on parsing json attributes in python? I would like to be able to parse the true value for "ok" field. As well as the index named "client_ind_1". I don't understand the python document coverage on this topic. If someone could explain or point me to a better resource, it would be awesome.

我的json字符串如下所示:

My json string looks like the below:

{
    "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
            }
        }
    }
}

谢谢.

推荐答案

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'和所需的索引值:

This will take json as python dictionary and will print 'ok' and index value you want:

True
{u'primary_size': u'2.5mb', u'primary_size_in_bytes': 2710326, u'size_in_bytes': 2710326, u'size': u'2.5mb'}

这篇关于在python中解析json字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆