Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JSON或Python dict/list解码问题_Python_Json_List_Dictionary_Nested - Fatal编程技术网

JSON或Python dict/list解码问题

JSON或Python dict/list解码问题,python,json,list,dictionary,nested,Python,Json,List,Dictionary,Nested,我一直在使用下面的Python脚本尝试从Flightradar24中检索和提取一些数据,它似乎以JSON格式提取数据,并使用JSON.dumps完全打印数据,但当我尝试选择所需数据时(本例中的状态文本)使用get会产生以下错误: “list”对象没有属性“get” 数据是JSON格式还是列表格式?我现在完全糊涂了 我对使用JSON格式的数据还比较陌生,如果有任何帮助,我将不胜感激 脚本: import flightradar24 import json flight_id = 'BA458'

我一直在使用下面的Python脚本尝试从Flightradar24中检索和提取一些数据,它似乎以JSON格式提取数据,并使用
JSON.dumps
完全打印数据,但当我尝试选择所需数据时(本例中的状态文本)使用
get
会产生以下错误:

“list”对象没有属性“get”

数据是JSON格式还是列表格式?我现在完全糊涂了

我对使用JSON格式的数据还比较陌生,如果有任何帮助,我将不胜感激

脚本:

import flightradar24
import json

flight_id = 'BA458' 
fr = flightradar24.Api()
flight = fr.get_flight(flight_id)

y = flight.get("data")
print (json.dumps(flight, indent=4))

X= (flight.get('result').get('response').get('data').get('status').get('text'))
print  (X)
输出数据示例:

{
"result": {
"request": {
"callback": null,
"device": null,
"fetchBy": "flight",
"filterBy": null,
"format": "json",
"limit": 25,
"page": 1,
"pk": null,
"query": "BA458",
"timestamp": null,
"token": null
},
"response": {
"item": {
"current": 16,
"total": null,
"limit": 25
},
"page": {
"current": 1,
"total": null
},
"timestamp": 1546241512,
"data": [
{
"identification": {
"id": null,
"row": 4852575431,
"number": {
"default": "BA458",
"alternative": null
},
"callsign": null,
"codeshare": null
},
"status": {
"live": false,
"text": "Scheduled",
"icon": null,
"estimated": null,
"ambiguous": false,
"generic": {
"status": {
"text": "scheduled",
"type": "departure",
"color": "gray",
"diverted": null
},
您可以使用
print(type(variable_name))
查看它是什么类型。列表上不支持-支持的是
dict

X = (flight.get('result').get('response').get('data').get('status').get('text'))
#                                            ^^^^^^^^ does not work, data is a list of dicts
as
data
是一个
dict
s的列表:


正如@PatrickArtner所指出的,问题在于你的
数据实际上是一个列表,而不是一本字典。另一方面,如果在嵌套字典上重复应用
dict.get
,您可能会发现代码更可读:

from functools import reduce

def ng(dataDict, mapList):
    """Nested Getter: Iterate nested dictionary"""
    return reduce(dict.get, mapList, dataDict)

X = ng(ng(flight, ['result', 'response', 'data'])[0], ['status'[, 'text']])

谢谢我要试一试。如果它能让我更容易地确定我想要的数据,那就值得了。
 X = (flight.get('result').get('response').get('data')[0].get('status').get('text')
from functools import reduce

def ng(dataDict, mapList):
    """Nested Getter: Iterate nested dictionary"""
    return reduce(dict.get, mapList, dataDict)

X = ng(ng(flight, ['result', 'response', 'data'])[0], ['status'[, 'text']])