我有我的JSON文件:
[
{
"order_":{
"id":"B2B78502",
"status_order_id":5,
"status_order_name":"Sent",
"customer_id":256,
"order_products":[
{
"order_product":{
"id":83630,
"order_id":79288,
"product_id":13519,
"quantity":"1.0",
"price":"72.44",
"addressbook_":{
"name":"user user",
"address":"adress",
"city":"THIENNES",
"country":"FR",
"postal_code":"59189",
"phone":"000000000"
},
"product_name":"product",
"product_code":"20159"
}
}
],
"customer_email":"user@gmail.com",
"customer_company":"SARL"
}
}
]
我想将数据插入MySQL表,但是我无法访问所有值
这是我的代码:
json_obj = r.json()
for ord in json_obj:
print("id:", ord["order"]["id"])
print("order_produvt_id:", ord["order"]["order_products"]"order_product"]["id"])
print('---')
我想访问所有数据我想访问所有数据,但出现此错误:
id: B2B78588
print("order_product_id:", ord["order"]["order_products"]["order_product"]["id"])
TypeError: list indices must be integers or slices, not str
答案 0 :(得分:0)
在“ order_products”侧,您有一个数组,由以下花括号指示:[]。
您需要使用Integers访问数组,就像错误告诉您的那样。
ord["order"]["order_products"][0]["order_product"]["id"]
上面的示例应该起作用。