私は以下のようなJSONオブジェクトを持っています。実際に巨大なJSONファイルは、この質問をするために簡略化されています。ネストされた辞書の中にあるアクセスキーPython
"header": {
"headerAttributes": {
"pageCount": 5,
"totalCount": 5
}
},
"payload": [
{
"firstKey": [
{
"nestedKey_1": "1",
"nestedKey_2": "2"
},
{
"nestedKey_3": "3",
"nestedKey_4": "4"
}
]
}
]
私は[]内の値にアクセスするために以下のpythonコードを書いています。
Total_Orders_Count = json_response ['header']['headerAttributes']['totalCount']
for i in range(0,Total_Orders_Count):
dict = json_response ['payload'][i]
inner_dict = dict ['firstKey'] [0]
print inner_dict ['nestedKey_1'] + '(' + inner_dict['nestedKey_2'] + ')'
上記のコードは正常に動作します。しかし、dict ['firstKey'] [0]の使用は1より大きい値では機能しません。
2つの質問。 1. []内のKey値にアクセスするには、より良い方法がありますか? 2. []内の値の長さを見つけることができますか?このリストでは、 "firstKey"の下の値の長さは2です。
何か助けていただければ幸いです。
「辞書の使用を[ 『のfirstKey』] [0]が動作しません1 "以上の値は?どういう意味ですか? –
また、あなたの変数 'dict'を呼び出さない –
' len(json_response ['payload'] [i] ['firstKey']) ' – Barmar