python - 用python读取嵌套的json数据
问题描述
我正在尝试从所有采购订单的 JSON 数据下方读取所有 ID。在下面的示例中,只有 2 个 id (523fbc6c-359a-49ea-81ea-065e20e4b1db + 77a5e074-5b23-4f85-989d-1b8152982c6e)。任何想法如何实现这一目标?
已经尝试了一些博客和代码片段,但结果还没有:) 可以使用下一个示例:
json_data = {
"purchaseOrder": [
{
"id": "523fbc6c-359a-49ea-81ea-065e20e4b1db",
"installationNumber": "test",
"peerId": "ac0fd195-cb24-4ced-a5fe-664a25651855",
"validFrom": "2019-05-28T14:57:21.000+0000",
"validTo": "2019-05-28T15:57:21.000+0000",
"originalQuantity": 20,
"quantity": 0,
"price": 5,
"periodInitial": "2019-05-28T15:00:00.000+0000",
"periodFinal": "2019-05-28T16:00:00.000+0000"
},
{
"id": "77a5e074-5b23-4f85-989d-1b8152982c6e",
"installationNumber": "test",
"peerId": "308866ba-90cb-47a7-8c73-589c0f355eb7",
"validFrom": "2019-05-28T14:57:21.000+0000",
"validTo": "2019-05-28T15:57:21.000+0000",
"originalQuantity": 20,
"quantity": 15,
"price": 5,
"periodInitial": "2019-05-28T15:00:00.000+0000",
"periodFinal": "2019-05-28T16:00:00.000+0000"
}
],
"salesOrder": [
{
"id": "7113f1ee-6980-4447-bf71-75c93a4c5bad",
"installationNumber": "test",
"peerId": "308866ba-90cb-47a7-8c73-589c0f355eb7",
"validFrom": "2019-05-28T14:57:21.000+0000",
"validTo": "2019-05-28T15:57:21.000+0000",
"originalQuantity": 20,
"quantity": 0,
"price": 5,
"periodInitial": "2019-05-28T15:00:00.000+0000",
"periodFinal": "2019-05-28T16:00:00.000+0000"
}
],
"agreement": [
{
"id": "e0f0ea4d4ecb357f443df720c457d8f20bcdc0b9d28b8eaa24a1b6bd80bd3ac50",
"installationNumber": "test",
"quantity": 15,
"price": 5,
"periodInitial": "2019-05-28T15:00:00.000+0000",
"periodFinal": "2019-05-28T16:00:00.000+0000",
"type": "A",
"status": "",
"agrPurchOrder": "523fbc6c-359a-49ea-81ea-065e20e4b1db",
"agrSalesOrder": "7113f1ee-6980-4447-bf71-75c93a4c5bad",
"creationDate": "2019-06-06T09:42:46.710+0000"
},
{
"id": "e0f0ea4d4ecb357f443df720c457d8f20bcdc0b9d28b8eaa24a1b6bd80bd3ac51",
"installationNumber": "test",
"quantity": 5,
"price": 5,
"periodInitial": "2019-05-28T15:00:00.000+0000",
"periodFinal": "2019-05-28T16:00:00.000+0000",
"type": "A",
"status": "",
"agrPurchOrder": "77a5e074-5b23-4f85-989d-1b8152982c6e",
"agrSalesOrder": "7113f1ee-6980-4447-bf71-75c93a4c5bad",
"creationDate": "2019-06-06T09:42:46.711+0000"
}
],
"status": ""
}
解决方案
使用列表推导:
result = [x['id'] for x in json_data['purchaseOrder']]
['523fbc6c-359a-49ea-81ea-065e20e4b1db', '77a5e074-5b23-4f85-989d-1b8152982c6e']
推荐阅读
- css - 滑块行高 CSS
- javascript - nodejs的io中的io.sockets.adapter.rooms在哪里?
- python - 简单的python脚本,多线程?
- node.js - Hyperledger Fabric v2.1:网关连接选项中 asLocalhost 设置为 false 的 Fabcar
- sql - 将日期时间与另一个表 date_time 进行比较
- node.js - 如何在猫鼬的集合中找到文档项的平均值?
- python - CSS 静态文件在 Django 中不起作用,我是 Django 的初学者,main.css 属性没有应用于我的 store.html
- java - 是否有一个正则表达式,如果第一个表达式有效,则检查下一个
- performance - 如何解决 JMeter 中的活动会话问题
- typescript - 使用 Deno 进行 JWT 身份验证