parsing json fields in python

后端 未结 2 1863
南方客
南方客 2021-02-06 05:08

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 d

2条回答
  •  闹比i
    闹比i (楼主)
    2021-02-06 05:28

    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'])
    

    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'}
    

提交回复
热议问题