简体   繁体   English

为什么我解析的JSON转换为列表python?

[英]Why is my parsed JSON converting into a list python?

I am trying to parse a JSON file from an api, and i am not sure why it's converting to a list. 我正在尝试从api解析JSON文件,但我不确定为什么它会转换为列表。

from urllib2 import Request, urlopen
import json
Auth_key = 'get ur own key'
headers = {
  'Authorization': Auth_key
}
request = Request('https://app.com/orders?orderStatus=awaiting_shipment', headers=headers)

def remove_nulls(d):
    d = {k: v for k, v in d.iteritems() if v is not None}
    return { k:v for k, v in d.items() if v }

response_body = urlopen(request).read()

parsed = json.loads(response_body, object_hook=remove_nulls)
jsonData = parsed["orders"]
#print(json.dumps(jsonData, indent=1, sort_keys=True))
print type(jsonData)

When I check "parsed" it's a dict but "jsonData" is not. 当我检查“解析”时,它是一个字典,而“ jsonData”则不是。 Thanks in advance 提前致谢

You are accessing a key from your dict so it can be any other datatype . 您正在从dict访问key ,因此它可以是任何其他datatype

This might be a case with you: 您可能会遇到这种情况:

In [91]: jsonData = {'orders': [1, 2, 3, 4], 'others': {}}

In [92]: type(jsonData['orders'])
Out[92]: list

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

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