Python与json

来源:互联网 发布:c语言中随机数怎么写 编辑:程序博客网 时间:2024/06/07 01:09

在处理json格式的数据时,python具备极其强大数据处理能力。


在面对多重嵌套的json数据比如

{"image_id": "GHWLpJmGOAeQ39kHK8JMLw==", "result": [{"child-objects": [{"child-objects": [], "position": [{"y": 129, "x": 194}, {"y": 127, "x": 237}, {"y": 172, "x": 238}, {"y": 173, "x": 194}], "type": "character", "value": "1"}], "position": [], "type": "textline", "value": "1"}, {"child-objects": [{"child-objects": [], "position": [{"y": 125, "x": 229}, {"y": 122, "x": 284}, {"y": 175, "x": 288}, {"y": 182, "x": 235}], "type": "character", "value": "6"}], "position": [], "type": "textline", "value": "6"}], "request_id": "1497860134,88b06a72-907a-4349-960a-f7ee5e78e3a4", "time_used": 1757}

我们首先可以使用python的json库将其格式化成json字符串

import jsonsrc={"image_id": "GHWLpJmGOAeQ39kHK8JMLw==", "result": [{"child-objects": [{"child-objects": [], "position": [{"y": 129, "x": 194}, {"y": 127, "x": 237}, {"y": 172, "x": 238}, {"y": 173, "x": 194}], "type": "character", "value": "1"}], "position": [], "type": "textline", "value": "1"}, {"child-objects": [{"child-objects": [], "position": [{"y": 125, "x": 229}, {"y": 122, "x": 284}, {"y": 175, "x": 288}, {"y": 182, "x": 235}], "type": "character", "value": "6"}], "position": [], "type": "textline", "value": "6"}], "request_id": "1497860134,88b06a72-907a-4349-960a-f7ee5e78e3a4", "time_used": 1757}dict=json.loads(src)

那么就能获得

{    "image_id": "GHWLpJmGOAeQ39kHK8JMLw==",    "result": [        {            "child-objects": [                {                    "child-objects": [],                    "position": [                        {                            "y": 129,                            "x": 194                        },                        {                            "y": 127,                            "x": 237                        },                        {                            "y": 172,                            "x": 238                        },                        {                            "y": 173,                            "x": 194                        }                    ],                    "type": "character",                    "value": "1"                }            ],            "position": [],            "type": "textline",            "value": "1"        },        {            "child-objects": [                {                    "child-objects": [],                    "position": [                        {                            "y": 125,                            "x": 229                        },                        {                            "y": 122,                            "x": 284                        },                        {                            "y": 175,                            "x": 288                        },                        {                            "y": 182,                            "x": 235                        }                    ],                    "type": "character",                    "value": "6"                }            ],            "position": [],            "type": "textline",            "value": "6"        }    ],    "request_id": "1497860134,88b06a72-907a-4349-960a-f7ee5e78e3a4",    "time_used": 1757}


那么如何获得第一个‘value’的值1呢
此时我们可以利用python的字典对象轻松的获取json数据的key与element

通过观察上述片段可以发现总共有三重字典嵌套,实际上是一个五重 字典-list 混合嵌套

第一重:

dict0_keys=dict.keys()print dict0_keys


得到第一重
[time_used', image_id', result', request_id']

让我们看看第二重‘result’的element,它们同样是由“key”与“element”组成只不过变成了list:
print dict1=dict['result']

[{child-objects': [{child-objects': [], position': [{y': 129, x': 194}, {y': 127, x': 237}, {y': 172, x': 238}, {y': 173, x': 194}], type': character', value': 1'}], position': [], type': textline', value': 1'}, {child-objects': [{child-objects': [], position': [{y': 125, x': 229}, {y': 122, x': 284}, {y': 175, x': 288}, {y': 182, x': 235}], type': character', value': 6'}], position': [], type': textline', value': 6'}]


只需要对list简单操作就可以获得key为'child-object'd的第三重,回到字典:
print dict2=dict1[0]
{'child-objects': [{'child-objects': [], 'position': [{'y': 129, 'x': 194}, {'y': 127, 'x': 237}, {'y': 172, 'x': 238}, {'y': 173, 'x': 194}], 'type': 'character', 'vale': '1'}], 'position': [], 'type': 'textline', 'vale': '1'}


再对对象‘child-objects’操作获得第四重:
print dict3=dict2['child-objects']
[{'child-objects': [], 'position': [{'y': 129, 'x': 194}, {'y': 127, 'x': 237}, {'y': 172, 'x': 238}, {'y': 173, 'x': 194}], 'type': 'character', 'vale': '1'}]


第五重:
print dict4=dict3[0]
{'child-objects': [], 'position': [{'y': 129, 'x': 194}, {'y': 127, 'x': 237}, {'y': 172, 'x': 238}, {'y': 173, 'x': 194}], 'type': 'character', 'vale': '1'}

最终获得第一个key为‘value’的element:
print dict4['value']

其他key的获取同理可得。
可见这是一个字典-list-字典-list-字典的循环,只需要了解嵌套层数,便可以轻松获得各个key值。

原创粉丝点击