이 코드에서 163, 203 & 인 특정 키를 선택하려고합니다. 13 기본적으로 my 함수가 실행될 때마다 다음 중 하나를 갖습니다. 3 변수에 코드 스톡을 저장하고 있기 때문에 Json 파일에서 특정 요소를 구문 분석하는 방법을 알 수 없습니다. 나는 열쇠 '203'에서 멈추고 그 결과를 돌려주는 고리를 만들고 싶습니다.JSON을 구문 분석하고 파이썬에서 특정 키를 선택하십시오.
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')가 있습니다. 나는이 이드의 가치를 얻을 필요가있다.
{
"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
}
}
}
질문을 자세히 기재 해주세요. 열쇠는 json tree의 어느 레벨 에나 존재할 수 있습니까? 정확히 인쇄해야하는 것은 무엇입니까? – nkprince007
내가 편집 한 질문은 나의 질문에 대한 질문이 더 명확합니까? – KenJ
다른 가격대의 가격에 바로 아래 키가 있습니다. –