简体   繁体   English

在python中解析嵌套的json

[英]Parsing nested json in python

I'm trying to parse the following json to get the Item (Item1, Item2 ...) and URL fields out of the payload. 我正在尝试解析以下json,以从有效负载中获取Item(Item1,Item2 ...)和URL字段。 I'm able to parse out the Item1, Item2 no problem with something like this but I'm at a loss of how to get the URL out of it as well 我可以解析出Item1,Item2像这样的东西,但是我也很困惑如何从中获取URL。

resp = requests.get(url)
data = json.loads(resp.text)
items = data["items"]
for item in items:
    print(item)

I've tried adding something like this just to get the subscriptions but receive KeyError: 0 我尝试添加这样的内容只是为了获得订阅但收到KeyError: 0

subscriptions = data["items"][0]

Payload: 有效载荷:

{
  "items": {
    "Item1": {
      "subscriptions": [
        {
          "url": "https://someurl.com",
          "name": "Customer1",
          "categories": [
            "Appointment"
          ]
        }
      ]
    },
    "Item2": {
      "subscriptions": [
        {
          "name": "Customer2",
          "url": "https://someotherurl.com",
          "authPass": "",
          "authUser": "",
          "categories": [
            "Appointment"
          ]
        }
      ]
    }
  }
}

只是继续挖

Item1_url= data["items"]["Item1"]["subscriptions"][0]['url']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM