迭代 json 数据包含许多 None 类型的对象

Iterating over json data consists of many None type objects

我正在尝试迭代 json 数据。这是我的数据结构

import requests
import re
url = "https://web.archive.org/__wb/calendarcaptures?url=http%3A%2F%2Fwww.unibocconi.it&selected_year=2014"
# You can see the data structure by copy-pasting the link
data = requests.get(url).json()
    for x in data:
       for y in x:
           for z in y:
               for xx in z:
                    start1 = "'ts': "
                    start2 = "'st': "
                    h = str(xx)
                    a = re.search('%s(.*)' % (start1) , h).group(1)
                    date = a[:16].replace("[", "").replace("]", "")
                    date = re.sub("[^0-9]", "", date)
                    b = re.search('%s(.*)' % (start2) , h).group(1)
                    status = b[:5].replace("[", "").replace("]", "")

我知道,我不能遍历 None 类型的对象。但是我几个小时都无法解决问题。有任何想法吗? 注意:我使用 requests

直接从网络获取 json 数据
json_acceptable_string = data.replace("'", "\"").replace('None', 'null')
d = json.loads(json_acceptable_string)

如果您真正想要的只是 count/statuscode/timestamp 值,则无需逐字解析 json 列表。 Python 将根据需要将 json 拉入 list/dict。 因此要超越任何 "None" 值,请使用 "if z:" 条件语句.

一旦到达 z 存在的位置,z.get('cnt','') 将拉出该字段(如果它在那里),或者 return 如果它不存在则什么也不做。然后,您可以使用 pop 进入 status/date 列表。我写那部分的方式不是很优雅,但它会完成工作。 (这假定 status/timestamp 列表的长度始终为 1。如果不是这种情况,您可以在其中插入一些其他 logic/indexing 以很容易地提取您感兴趣的值。)

for x in data:
    for y in x:
        for z in y:
            if z:
                count = z.get('cnt', '')
                st = z.get('st', '')
                if st:
                    status = st.pop()
                ts = z.get('ts', '')
                if ts:
                    date = ts.pop()

print(count, status, date)

2 200 20140308061038

更新:数据是列表类型。