gpt4 book ai didi

mysql - 使用 python 访问 json 值

转载 作者:行者123 更新时间:2023-11-30 21:40:54 25 4
gpt4 key购买 nike

我有我的 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

最佳答案

在“order_products”一侧,您有一个数组,由这些大括号表示:[ ]。

您需要使用整数访问数组,就像错误告诉您的那样。

ord["order"]["order_products"][0]["order_product"]["id"]

上面的例子应该可行。

关于mysql - 使用 python 访问 json 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51673628/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com