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}
此时我们可以利用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值。
阅读全文
0 0
- python与json
- python对象与json
- Python与json
- python json.dumps() 与 json.dump()
- python json.dump()与json.load()
- python与json的转换
- python dict与json转换
- python解析与组装json
- 【Python】python对象与json相互转换
- 【Python】python对象与json相互转换
- python JSON性能测试与simplejson对比
- python数据类型与json的互相转换
- python中Json与object转化
- Python基础-JSON编码与解码
- python对象与json相互转换
- python json的dumps与loads
- Python字典与json的异同
- Python-json与pickle数据序列化
- 版本控制工具SVN的使用方法
- iOS 支持https
- 管理数据的价值
- Inverse是hibernate双向关系中的基本概念
- 怎样在java程序中对项目进行硬件检测
- Python与json
- html5语法
- Unite 2017 | Unity引擎发展四大方向
- Spring AOP 深入剖析 (再学习)
- android MVP的holder变异
- 【JAVA
- 使用ShareSDK三方登陆
- “免密登录”——开拓便捷登录新时代
- Android6.0动态权限申请