我正在尝试下载一个json文件,保存该文件,并遍历该json文件,以便提取所有信息并将其保存为变量。然后,我将以csv格式格式化一条消息,以便将数据发送到另一个系统。我的问题是json数据。它看起来像是列表中的字典,我不确定如何处理它。
下面是json:
[ {
"ipAddress" : "",
"feedDescription" : "Botted Node Feed",
"bnFeedVersion" : "1.1.4",
"generatedTs" : "2013-08-01 12:00:10.360+0000",
"count" : 642903,
"firstDiscoveredTs" : "2013-07-21 19:07:20.627+0000",
"lastDiscoveredTs" : "2013-08-01 00:34:41.052+0000",
"threatType" : "BN",
"confidence" : 82,
"discoveryMethod" : "spamtrap",
"indicator" : true,
"supportingData" : {
"behavior" : "spamming",
"botnetName" : null,
"spamtrapData" : {
"uniqueSubjectCount" : 88
},
"p2pData" : {
"connect" : null,
"port" : null
}
}
}, {
"ipAddress" : "",
"feedDescription" : "Botted Node Feed",
"bnFeedVersion" : "1.1.4",
"generatedTs" : "2013-08-01 12:00:10.360+0000",
"count" : 28,
"firstDiscoveredTs" : "2013-07-19 03:19:08.622+0000",
"lastDiscoveredTs" : "2013-08-01 01:44:04.009+0000",
"threatType" : "BN",
"confidence" : 40,
"discoveryMethod" : "spamtrap",
"indicator" : true,
"supportingData" : {
"behavior" : "spamming",
"botnetName" : null,
"spamtrapData" : {
"uniqueSubjectCount" : 9
},
"p2pData" : {
"connect" : null,
"port" : null
}
}
}, {
"ipAddress" : "",
"feedDescription" : "Botted Node Feed",
"bnFeedVersion" : "1.1.4",
"generatedTs" : "2013-08-01 12:00:10.360+0000",
"count" : 160949,
"firstDiscoveredTs" : "2013-07-16 18:52:33.881+0000",
"lastDiscoveredTs" : "2013-08-01 03:14:59.452+0000",
"threatType" : "BN",
"confidence" : 82,
"discoveryMethod" : "spamtrap",
"indicator" : true,
"supportingData" : {
"behavior" : "spamming",
"botnetName" : null,
"spamtrapData" : {
"uniqueSubjectCount" : 3
},
"p2pData" : {
"connect" : null,
"port" : null
}
}
} ]我的代码:
download = 'https:URL.BNfeed20130801.json'
request = requests.get(download, verify=False)
out = open(fileName, 'w')
for row in request:
if row.strip():
for column in row:
out.write(column)
else:
continue
out.close()
time.sleep(4)
jsonRequest = request.json()
for item in jsonRequest:
print jsonRequest[0]['ipAddress']
print jsonRequest[item]['ipAddress'] --I also tried this当我执行上面的操作时,它只是一遍又一遍地打印相同的IP。我放入print语句只是为了测试。一旦我弄清楚要访问JSON的不同元素,我将把它存储在变量中,然后相应地使用这些变量。任何帮助都是非常感谢的。
提前感谢您的帮助。我在Linux上使用Python 2.6。
发布于 2013-08-20 03:33:38
你基本上是在遍历字典列表,试一下item['ipAddress']。
发布于 2013-08-20 04:06:51
alecxe的答案告诉你如何修复这个问题,但让我来解释一下原始代码的问题所在。
一旦可以通过interactive visualizer运行,使用更简单的示例可能更容易理解
a = ['a', 'b', 'c']执行此操作时:
for item in a:item将在第一次通过时被'a',然后是'b',然后是'c'。
但是如果你这样做:
for item in a:
print a[0]…“你完全忽略了item。它只会打印a 3次,因为每次循环,你都会要求a[0]-that is,a中的第一件事。
如果你这样做:
for item in a:
print a[item]…它将引发异常,因为您请求的是列表中的第'a'项,这是无稽之谈。
但在下面的代码中:
for item in a:
print item…您将依次打印'a'、'b'和'c',这正是您想要的。
你也可以这样做:
for index, item in enumerate(a):
print a[index]…但那是愚蠢的。如果需要索引,请使用enumerate,但如果只需要项目本身的…你已经得到它了。
所以,回到你真正的代码:
for item in jsonRequest:
print jsonRequest[0]['ipAddress']又一次,您忽略了item,并且每次都请求jsonRequest[0]。
在下面的代码中:
for item in jsonRequest:
print jsonRequest[item]['ipAddress'] --I also tried this…您在jsonRequest中请求的是{complicated dictionary}th,这又是一派胡言。
但在下面的代码中:
for item in jsonRequest:
print item['ipAddress']您正在使用每个项目,就像在这个简单的示例中一样。
https://stackoverflow.com/questions/18321656
复制相似问题