Python 对json数据的迭代由许多非类型对象组成

Python 对json数据的迭代由许多非类型对象组成,python,json,iteration,nonetype,Python,Json,Iteration,Nonetype,我试图迭代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()

我试图迭代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("]", "")
我知道,我不能迭代非类型对象。但是我有几个小时没能解决这个问题。有什么想法吗?
注意:我通过使用请求直接从web获取json数据

如果您真正想要的是count/statuscode/timestamp值,则不需要逐字解析json列表。Python将根据需要将json作为list/dict拉入。因此,要通过任何“None”值,请使用“if z:”条件语句

json_acceptable_string = data.replace("'", "\"").replace('None', 'null')
d = json.loads(json_acceptable_string)
一旦到达z存在的位置,z.get('cnt','')将在字段存在时提取该字段,如果字段不存在则不返回任何内容。然后,您可以使用pop进入状态/日期列表。我写那部分的方式不是很优雅,但它会完成工作的。(这假设状态/时间戳列表的长度始终为1。如果不是这样,您可以很容易地在其中插入一些其他逻辑/索引,以提取您感兴趣的值。)


更新:数据属于列表类型。

@pault它似乎是从他的代码的第一行开始定义的,该行的开头是,
Data=
None
在JSON中无效。它应该是
null
。事实上这是我得到的,但没有一个可能是字符串…@edyvedy13可能是?也许你应该检查确认一下。不管怎样,你们到底想对数据做什么?你们不能在字符串上做嵌套循环
x
正在对字符进行迭代,因此没有什么可迭代的了。下面是示例数据,它不是json?您不断更改代码,您原来有一个str。正如您现在拥有的,应该可以工作。
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