すべての情報を抽出して変数として保存するために、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が何度も表示されます。テスト目的でのみ印刷ステートメントを入れました。JSON のさまざまな要素にアクセスする方法がわかったら、それを変数に格納し、それに応じてこれらの変数を使用します。どんな助けでも大歓迎です。
助けてくれてありがとう。LinuxでPython 2.6を使用しています。