解析 JSON 和 select python 中的特定键
Parse JSON and select a specific key in python
我正在尝试 select 此代码中的一个特定键,即 163、203 和 13。基本上,每次我的函数运行时,我都会存入这 3 个代码之一一个变量,我不知道如何解析 Json 文件中的特定元素。我想做一个循环,例如在键 '203' 处停止,然后 return 我得到它的结果。
with open('data.json') as data_file:
data = json.load(data_file, object_pairs_hook=OrderedDict)
for key, value in data.items():
id = '203'
id_value = value['payload']['price']
pack = id_value[id]
print(pack) #pack should be returning the value of the key '203'
这是 JSON。 JSON 分为 3 个部分,每个部分都以一个我不知道的随机密钥开头,如果你查看 JSON,在有效载荷之后,是价格,然后是ID(例如“203”)。我需要获取这个id的值。
{
"a506b5686": {
"status": {
"message": "Ok",
"code": 0,
"user": {
"isBanned": false,
"isNotConfirmed": false
}
},
"payload": {
"vat": 0,
"price": {
"163": {
"100": {
"country_id": "CN",
"product_price": 16.54,
"shipment_price": 2.31,
"quantity": 100,
"batchsize_id": 60348
},
"200": {
"country_id": "CN",
"product_price": 23.17,
"shipment_price": 2.31,
"quantity": 200,
"batchsize_id": 60354
},
"300": {
"country_id": "CN",
"product_price": 28.29,
"shipment_price": 2.31,
"quantity": 300,
"batchsize_id": 60360
},
"400": {
"country_id": "CN",
"product_price": 34.11,
"shipment_price": 2.31,
"quantity": 400,
"batchsize_id": 60366
},
"500": {
"country_id": "CN",
"product_price": 39.33,
"shipment_price": 2.31,
"quantity": 500,
"batchsize_id": 60372
},
"600": {
"country_id": "CN",
"product_price": 44.04,
"shipment_price": 2.95,
"quantity": 600,
"batchsize_id": 60378
},
"700": {
"country_id": "CN",
"product_price": 48.32,
"shipment_price": 2.95,
"quantity": 700,
"batchsize_id": 60384
},
"800": {
"country_id": "CN",
"product_price": 52.22,
"shipment_price": 2.95,
"quantity": 800,
"batchsize_id": 60390
},
"900": {
"country_id": "CN",
"product_price": 55.79,
"shipment_price": 2.95,
"quantity": 900,
"batchsize_id": 60396
},
"1000": {
"country_id": "CN",
"product_price": 59.07,
"shipment_price": 2.95,
"quantity": 1000,
"batchsize_id": 60402
},
"maxDeliveryDays": 6,
"minDeliveryDays": 4
}
},
"maxDeliveryDays": 6,
"minDeliveryDays": 4,
"productionCountry": [
"CN"
]
},
"pager": {
"total": null,
"current": 1,
"count": null
}
},
"a28bb008b": {
"status": {
"message": "Ok",
"code": 0,
"user": {
"isBanned": false,
"isNotConfirmed": false
}
},
"payload": {
"vat": 0,
"price": {
"203": {
"100": {
"country_id": "CN",
"product_price": 16.21,
"shipment_price": 2.31,
"quantity": 100,
"batchsize_id": 60972
},
"200": {
"country_id": "CN",
"product_price": 22.58,
"shipment_price": 2.31,
"quantity": 200,
"batchsize_id": 60978
},
"300": {
"country_id": "CN",
"product_price": 27.5,
"shipment_price": 2.31,
"quantity": 300,
"batchsize_id": 60984
},
"400": {
"country_id": "CN",
"product_price": 33.11,
"shipment_price": 2.31,
"quantity": 400,
"batchsize_id": 60990
},
"500": {
"country_id": "CN",
"product_price": 38.14,
"shipment_price": 2.31,
"quantity": 500,
"batchsize_id": 60996
},
"600": {
"country_id": "CN",
"product_price": 42.69,
"shipment_price": 2.31,
"quantity": 600,
"batchsize_id": 61002
},
"700": {
"country_id": "CN",
"product_price": 46.82,
"shipment_price": 2.95,
"quantity": 700,
"batchsize_id": 61008
},
"800": {
"country_id": "CN",
"product_price": 50.58,
"shipment_price": 2.95,
"quantity": 800,
"batchsize_id": 61014
},
"900": {
"country_id": "CN",
"product_price": 54.02,
"shipment_price": 2.95,
"quantity": 900,
"batchsize_id": 61020
},
"1000": {
"country_id": "CN",
"product_price": 57.18,
"shipment_price": 2.95,
"quantity": 1000,
"batchsize_id": 61026
},
"maxDeliveryDays": 6,
"minDeliveryDays": 4
}
},
"maxDeliveryDays": 6,
"minDeliveryDays": 4,
"productionCountry": [
"CN"
]
},
"pager": {
"total": null,
"current": 1,
"count": null
}
},
"a2e2a89bf": {
"status": {
"message": "Ok",
"code": 0,
"user": {
"isBanned": false,
"isNotConfirmed": false
}
},
"payload": {
"vat": 0,
"price": {
"13": {
"100": {
"country_id": "CN",
"product_price": 14.95,
"shipment_price": 2.31,
"quantity": 100,
"batchsize_id": 58240
},
"200": {
"country_id": "CN",
"product_price": 20.37,
"shipment_price": 2.31,
"quantity": 200,
"batchsize_id": 58226
},
"300": {
"country_id": "CN",
"product_price": 24.57,
"shipment_price": 2.31,
"quantity": 300,
"batchsize_id": 58237
},
"400": {
"country_id": "CN",
"product_price": 29.41,
"shipment_price": 2.31,
"quantity": 400,
"batchsize_id": 58228
},
"500": {
"country_id": "CN",
"product_price": 33.76,
"shipment_price": 2.31,
"quantity": 500,
"batchsize_id": 58234
},
"600": {
"country_id": "CN",
"product_price": 37.69,
"shipment_price": 2.95,
"quantity": 600,
"batchsize_id": 58235
},
"700": {
"country_id": "CN",
"product_price": 41.25,
"shipment_price": 2.95,
"quantity": 700,
"batchsize_id": 58264
},
"800": {
"country_id": "CN",
"product_price": 44.5,
"shipment_price": 2.95,
"quantity": 800,
"batchsize_id": 58270
},
"900": {
"country_id": "CN",
"product_price": 47.46,
"shipment_price": 2.95,
"quantity": 900,
"batchsize_id": 58239
},
"1000": {
"country_id": "CN",
"product_price": 50.2,
"shipment_price": 2.95,
"quantity": 1000,
"batchsize_id": 58223
},
"maxDeliveryDays": 6,
"minDeliveryDays": 4
}
},
"maxDeliveryDays": 6,
"minDeliveryDays": 4,
"productionCountry": [
"CN"
]
},
"pager": {
"total": null,
"current": 1,
"count": null
}
}
}
在访问它之前检查它是否存在有什么问题。
import json
from collections import OrderedDict
id = '203'
with open('data,json') as data_file:
data = json.load(data_file, object_pairs_hook=OrderedDict)
for key, value in data.items():
id_value = value['payload']['price']
if id in id_value: # check first if id is present
pack = id_value[id]
print(pack) #pack should be returning the value of the key '203'
with open('data.json') as data_file:
data = json.load(data_file, object_pairs_hook=OrderedDict)
for key, value in data.items():
id = '203'
if value['payload']['price'] == id:
print(value['payload']['price'])
这是我从你的问题中得出的结论。如果这不是预期的,请在下面发表评论。
我尝试将 JSON 对象展平,以便所有键都位于同一水平面上。
import json
from pandas.io.json import json_normalize
from collections import OrderedDict
with open('data.json') as data_file:
data = json.load(data_file, object_pairs_hook=OrderedDict)
data=json_normalize(data)
id='163'
for key, value in data.items():
nested_ids = key.split('.')
if (id in nested_ids):
print(value)
这是否让您朝着正确的方向前进?
我正在尝试 select 此代码中的一个特定键,即 163、203 和 13。基本上,每次我的函数运行时,我都会存入这 3 个代码之一一个变量,我不知道如何解析 Json 文件中的特定元素。我想做一个循环,例如在键 '203' 处停止,然后 return 我得到它的结果。
with open('data.json') as data_file:
data = json.load(data_file, object_pairs_hook=OrderedDict)
for key, value in data.items():
id = '203'
id_value = value['payload']['price']
pack = id_value[id]
print(pack) #pack should be returning the value of the key '203'
这是 JSON。 JSON 分为 3 个部分,每个部分都以一个我不知道的随机密钥开头,如果你查看 JSON,在有效载荷之后,是价格,然后是ID(例如“203”)。我需要获取这个id的值。
{
"a506b5686": {
"status": {
"message": "Ok",
"code": 0,
"user": {
"isBanned": false,
"isNotConfirmed": false
}
},
"payload": {
"vat": 0,
"price": {
"163": {
"100": {
"country_id": "CN",
"product_price": 16.54,
"shipment_price": 2.31,
"quantity": 100,
"batchsize_id": 60348
},
"200": {
"country_id": "CN",
"product_price": 23.17,
"shipment_price": 2.31,
"quantity": 200,
"batchsize_id": 60354
},
"300": {
"country_id": "CN",
"product_price": 28.29,
"shipment_price": 2.31,
"quantity": 300,
"batchsize_id": 60360
},
"400": {
"country_id": "CN",
"product_price": 34.11,
"shipment_price": 2.31,
"quantity": 400,
"batchsize_id": 60366
},
"500": {
"country_id": "CN",
"product_price": 39.33,
"shipment_price": 2.31,
"quantity": 500,
"batchsize_id": 60372
},
"600": {
"country_id": "CN",
"product_price": 44.04,
"shipment_price": 2.95,
"quantity": 600,
"batchsize_id": 60378
},
"700": {
"country_id": "CN",
"product_price": 48.32,
"shipment_price": 2.95,
"quantity": 700,
"batchsize_id": 60384
},
"800": {
"country_id": "CN",
"product_price": 52.22,
"shipment_price": 2.95,
"quantity": 800,
"batchsize_id": 60390
},
"900": {
"country_id": "CN",
"product_price": 55.79,
"shipment_price": 2.95,
"quantity": 900,
"batchsize_id": 60396
},
"1000": {
"country_id": "CN",
"product_price": 59.07,
"shipment_price": 2.95,
"quantity": 1000,
"batchsize_id": 60402
},
"maxDeliveryDays": 6,
"minDeliveryDays": 4
}
},
"maxDeliveryDays": 6,
"minDeliveryDays": 4,
"productionCountry": [
"CN"
]
},
"pager": {
"total": null,
"current": 1,
"count": null
}
},
"a28bb008b": {
"status": {
"message": "Ok",
"code": 0,
"user": {
"isBanned": false,
"isNotConfirmed": false
}
},
"payload": {
"vat": 0,
"price": {
"203": {
"100": {
"country_id": "CN",
"product_price": 16.21,
"shipment_price": 2.31,
"quantity": 100,
"batchsize_id": 60972
},
"200": {
"country_id": "CN",
"product_price": 22.58,
"shipment_price": 2.31,
"quantity": 200,
"batchsize_id": 60978
},
"300": {
"country_id": "CN",
"product_price": 27.5,
"shipment_price": 2.31,
"quantity": 300,
"batchsize_id": 60984
},
"400": {
"country_id": "CN",
"product_price": 33.11,
"shipment_price": 2.31,
"quantity": 400,
"batchsize_id": 60990
},
"500": {
"country_id": "CN",
"product_price": 38.14,
"shipment_price": 2.31,
"quantity": 500,
"batchsize_id": 60996
},
"600": {
"country_id": "CN",
"product_price": 42.69,
"shipment_price": 2.31,
"quantity": 600,
"batchsize_id": 61002
},
"700": {
"country_id": "CN",
"product_price": 46.82,
"shipment_price": 2.95,
"quantity": 700,
"batchsize_id": 61008
},
"800": {
"country_id": "CN",
"product_price": 50.58,
"shipment_price": 2.95,
"quantity": 800,
"batchsize_id": 61014
},
"900": {
"country_id": "CN",
"product_price": 54.02,
"shipment_price": 2.95,
"quantity": 900,
"batchsize_id": 61020
},
"1000": {
"country_id": "CN",
"product_price": 57.18,
"shipment_price": 2.95,
"quantity": 1000,
"batchsize_id": 61026
},
"maxDeliveryDays": 6,
"minDeliveryDays": 4
}
},
"maxDeliveryDays": 6,
"minDeliveryDays": 4,
"productionCountry": [
"CN"
]
},
"pager": {
"total": null,
"current": 1,
"count": null
}
},
"a2e2a89bf": {
"status": {
"message": "Ok",
"code": 0,
"user": {
"isBanned": false,
"isNotConfirmed": false
}
},
"payload": {
"vat": 0,
"price": {
"13": {
"100": {
"country_id": "CN",
"product_price": 14.95,
"shipment_price": 2.31,
"quantity": 100,
"batchsize_id": 58240
},
"200": {
"country_id": "CN",
"product_price": 20.37,
"shipment_price": 2.31,
"quantity": 200,
"batchsize_id": 58226
},
"300": {
"country_id": "CN",
"product_price": 24.57,
"shipment_price": 2.31,
"quantity": 300,
"batchsize_id": 58237
},
"400": {
"country_id": "CN",
"product_price": 29.41,
"shipment_price": 2.31,
"quantity": 400,
"batchsize_id": 58228
},
"500": {
"country_id": "CN",
"product_price": 33.76,
"shipment_price": 2.31,
"quantity": 500,
"batchsize_id": 58234
},
"600": {
"country_id": "CN",
"product_price": 37.69,
"shipment_price": 2.95,
"quantity": 600,
"batchsize_id": 58235
},
"700": {
"country_id": "CN",
"product_price": 41.25,
"shipment_price": 2.95,
"quantity": 700,
"batchsize_id": 58264
},
"800": {
"country_id": "CN",
"product_price": 44.5,
"shipment_price": 2.95,
"quantity": 800,
"batchsize_id": 58270
},
"900": {
"country_id": "CN",
"product_price": 47.46,
"shipment_price": 2.95,
"quantity": 900,
"batchsize_id": 58239
},
"1000": {
"country_id": "CN",
"product_price": 50.2,
"shipment_price": 2.95,
"quantity": 1000,
"batchsize_id": 58223
},
"maxDeliveryDays": 6,
"minDeliveryDays": 4
}
},
"maxDeliveryDays": 6,
"minDeliveryDays": 4,
"productionCountry": [
"CN"
]
},
"pager": {
"total": null,
"current": 1,
"count": null
}
}
}
在访问它之前检查它是否存在有什么问题。
import json
from collections import OrderedDict
id = '203'
with open('data,json') as data_file:
data = json.load(data_file, object_pairs_hook=OrderedDict)
for key, value in data.items():
id_value = value['payload']['price']
if id in id_value: # check first if id is present
pack = id_value[id]
print(pack) #pack should be returning the value of the key '203'
with open('data.json') as data_file:
data = json.load(data_file, object_pairs_hook=OrderedDict)
for key, value in data.items():
id = '203'
if value['payload']['price'] == id:
print(value['payload']['price'])
这是我从你的问题中得出的结论。如果这不是预期的,请在下面发表评论。
我尝试将 JSON 对象展平,以便所有键都位于同一水平面上。
import json
from pandas.io.json import json_normalize
from collections import OrderedDict
with open('data.json') as data_file:
data = json.load(data_file, object_pairs_hook=OrderedDict)
data=json_normalize(data)
id='163'
for key, value in data.items():
nested_ids = key.split('.')
if (id in nested_ids):
print(value)
这是否让您朝着正确的方向前进?