mysql - 使用 python 访问 json 值

标签 mysql json python-3.x

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

相关文章:

php - MySQL Session 变量在触发器中的作用

php - MySql 时间戳 - 更新所有行后的不同时间

c# - Odata Asp.NET WebApi 在实体/DTO 中返回 json 属性

python - 用元类继承类字典

mysql - 疯狂的数据库结构 - 建议

php - MySQL在(结构相同)多个表中搜索id

mysql - 如何将带有列表的 JSON 数据转换为使用动态列的 MariaDB SQL Insert 语句

javascript - C# 对象到 Javascript 对象

python - Slack 发送附件返回 500 错误

python - 在 Heroku 上使用环境变量作为凭证