如何从嵌套数组中获取值? JSON

How to get a value from nested arrays? JSON

我在解析票证时遇到问题。有一个 JSON link 包含以下数据: json file

{"success":true,"data":{"AAE":{"2":{"price":48973,"airline":"AF","flight_number":1745,"departure_at":"2018-09-04T18:45:00Z","return_at":"2018-09-14T07:15:00Z","expires_at":"2018-09-02T06:57:21Z"},"3":{"price":67240,"airline":"TP","flight_number":1235,"departure_at":"2018-09-04T07:15:00Z","return_at":"2018-09-14T07:15:00Z","expires_at":"2018-09-02T06:57:21Z"}},"AAH":{"1":{"price":34049,"airline":"LH","flight_number":1453,"departure_at":"2018-09-30T09:05:00Z","return_at":"2018-10-02T07:40:00Z","expires_at":"2018-09-03T11:37:06Z"},"2":{"price":35838,"airline":"LH","flight_number":1453,"departure_at":"2018-09-30T09:05:00Z","return_at":"2018-10-02T11:39:00Z","expires_at":"2018-09-03T11:37:06Z"}},"AAL":{"1":{"price":23258,"airline":"KL","flight_number":904,"departure_at":"2018-12-08T18:00:00Z","return_at":"2018-12-15T06:00:00Z","expires_at":"2018-09-03T13:27:58Z"},"2":{"price":21867,"airline":"AF","flight_number":1745,"departure_at":"2018-12-08T20:00:00Z","return_at":"2018-12-15T18:15:00Z","expires_at":"2018-09-03T13:27:58Z"},"3":{"price":30639,"airline":"AF","flight_number":1145,"departure_at":"2018-12-08T09:45:00Z","return_at":"2018-12-15T06:00:00Z","expires_at":"2018-09-03T13:27:58Z"}},"AAQ":{"0":{"price":5354,"airline":"FV","flight_number":5515,"departure_at":"2018-09-16T04:20:00Z","return_at":"2018-10-04T12:40:00Z","expires_at":"2018-08-31T20:53:40Z"},"1":{"price":8590,"airline":"FV","flight_number":5515,"departure_at":"2018-09-16T04:20:00Z","return_at":"2018-10-04T15:05:00Z","expires_at":"2018-08-31T20:53:40Z"},"2":{"price":13702,"airline":"U6","flight_number":79,"departure_at":"2018-10-04T11:20:00Z","return_at":"2018-10-10T12:40:00Z","expires_at":"2018-09-03T06:47:01Z"}},"AAR":{"1":{"price":24418,"airline":"OK","flight_number":905,"departure_at":"2018-09-19T22:10:00Z","return_at":"2018-09-25T09:35:00Z","expires_at":"2018-09-02T21:16:33Z"},"2":{"price":20805,"airline":"AY","flight_number":712,"departure_at":"2018-09-19T11:50:00Z","return_at":"2018-09-25T16:55:00Z","expires_at":"2018-09-02T21:16:33Z"},"3":{"price":36316,"airline":"BT","flight_number":425,"departure_at":"2018-09-19T09:45:00Z","return_at":"2018-09-25T09:35:00Z","expires_at":"2018-09-02T21:16:33Z"}},

我的代码:

import json
from urllib.request import Request, urlopen

request = Request("http://api.link")
response_body = urlopen(request).read()

tickets = json.loads(response_body)

for item in tickets['data']:
    price = item['AAL']['1']['price']
    print(price)

我尝试了各种选项来获取 price 的值,但没有任何反应。

帮我获取这个值

我认为这些“1”和“2”是list而不是dict的项目。你需要遍历它们。尝试像这里这样处理:

import json
from urllib.request import Request, urlopen

request = Request("http://api.link")
response_body = urlopen(request).read()

tickets = json.loads(response_body)

for key, items in tickets['data'].items():
    for item in items:
        price = item['price']
        print(price)

我没有尝试使用您(最终)在问题中添加的原始数据——尽管仍然不是我建议的文本。相反,我手动输入了您最初发布的图片中显示的片段(并修复了格式)。

无论如何,下面的内容至少应该很接近,并向您展示了如何访问数据结构的各个部分:

response_body = '''
{
    "success": true,
    "data": {
        "AAL": {
            "1": {
                "price": 53411,
                "airline": "KL",
                "flight_number": 904,
                "departure_at": "2018-08-31T17:00:002",
                "return_at": "2018-09-01T18:20:002",
                "expires_at": "2018-08-31T17:00:OOZ"
            },
            "2": {
                "price": 56035,
                "airline": "BT",
                "flight_number": 429,
                "departure_at": "2018-08-31T15:25:002",
                "return_at": "2018-09-01To6:35:002",
                "expires_at": "2018-08-31T15:25:OOZ"
            }
        },
        "AAQ": {
            "0": {
                "price": 6242,
                "airline": "DP",
                "flight_number": 147,
                "departure_at": "2018-09-15T21:00:002",
                "return_at": "2018-10-05T12:40:002",
                "expires_at": "2018-09-03T13:18:222"
            }
        }
    }
}'''

处理数据的代码:

import json

tickets = json.loads(response_body)

for airport, flights in tickets['data'].items():
    print('airport:', airport)
    for id, flight in flights.items():
        print('  id:', id)
        print('    airline:', flight['airline'])
        print('    flight_number:', flight['flight_number'])
        print('    price:', flight['price'])  # Here's how to get the price.

它正在将 item 转换为 for 循环中的字符串。所以当你调用 ['1'] 时它会抛出一个错误,就好像你将它设置为一个整数,即 [1] 它会带回键的第二个字符,在这种情况下 'A'来自 'AAL'。如果您 mod 它满足您的需要,这将起作用。

import json
from urllib.request import Request, urlopen

request = Request("http://api.travelpayouts.com/v1/prices/cheap?origin=MOW&page=1&      token=77e84779fa444c14806f022f6c41b7fe")
response_body = urlopen(request).read()

tickets = json.loads(response_body)

for item in tickets['data']:
    if item == 'AAL':
        price = tickets['data'][item]['1']['price']

print(price)