[英]Printing Specific Value from Multi Nested Dict
(Very new to Python) I have a multi-nested dict in JSON format and I'm trying to check if a specific key value or true or false. (对 Python 来说非常新)我有一个 JSON 格式的多嵌套字典,我正在尝试检查特定键值是真还是假。 I'm unsure of the syntax to check for a key nested within several dicts.
我不确定检查嵌套在多个字典中的键的语法。 I am using the output of URLVoid's API which looks something like this:
我正在使用 URLVoid 的 API 的 output,它看起来像这样:
"data": {
"report": {
"dns_records": {
"ns": {
"records": [
{
"target": "alexis.ns.cloudflare.com",
"ip": "x.x.x.x",
"country_code": "US",
"country_name": "United States of America",
"isp": "CloudFlare Inc."
},
{
"target": "vida.ns.cloudflare.com",
"ip": "x.x.x.x",
"country_code": "JP",
"country_name": "Japan",
"isp": "CloudFlare Inc."
}
]
},
"mx": {
"records": []
}
},
"domain_blacklist": {
"engines": [
{
"name": "SpamhausDBL",
"reference": "https://www.spamhaus.org/lookup/",
"detected": false
},
{
"name": "ThreatLog",
"reference": "https://www.threatlog.com/",
"detected": false
},
{
"name": "OpenPhish",
"reference": "https://www.openphish.com/",
"detected": false
},
{
"name": "PhishTank",
"reference": "https://www.phishtank.com/",
"detected": false
},
{
"name": "Phishing.Database",
"reference": "https://github.com/mitchellkrogza/Phishing.Database",
"detected": false
},
{
"name": "PhishStats",
"reference": "https://phishstats.info/",
"detected": false
},
{
"name": "URLVir",
"reference": "https://www.urlvir.com/",
"detected": false
},
{
"name": "URLhaus",
"reference": "https://urlhaus.abuse.ch/",
"detected": false
},
{
"name": "RPiList Not Serious",
"reference": "https://github.com/RPiList/specials",
"detected": false
},
{
"name": "precisionsec",
"reference": "https://precisionsec.com/",
"detected": false
},
{
"name": "AntiSocial Blacklist",
"reference": "https://theantisocialengineer.com/",
"detected": false
},
{
"name": "PhishFeed",
"reference": "https://phishfeed.com/",
"detected": false
},
{
"name": "Spam404",
"reference": "https://www.spam404.com/",
"detected": false
},
{
"name": "CRDF",
"reference": "https://threatcenter.crdf.fr/check.html",
"detected": true
},
{
"name": "Artists Against 419",
"reference": "http://wiki.aa419.org/index.php/Main_Page",
"detected": false
},
{
"name": "CERT Polska",
"reference": "https://www.cert.pl/",
"detected": false
}
],
"detections": 1
To test, I have the JSON saved in my working directory.为了测试,我将 JSON 保存在我的工作目录中。 I try
我试试
f = open("results.json", "r")
#print(f.read())
print(f.read()["data"]["reports"]["domain_blacklist"]["engines"][0])
Error:错误:
Traceback (most recent call last):
File "directory/path", line 19, in <module>
print(f.read()["data"]["reports"]["domain_blacklist"]["engines"][0])
TypeError: string indices must be integers
What is the proper syntax to go through the data and check if the value of detected
is true
?通过数据检查 go 的正确语法是什么,并检查
detected
的值是否为true
? I understand for a simple dict like: a_dict = {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'}
I can simply do我理解一个简单的字典,例如:
a_dict = {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'}
我可以简单地做
for key in dict:
print key
But because this is multi-nested I am getting lost.但是因为这是多嵌套的,所以我迷路了。
After reading your JSON file you need json.load(fp)
to deserialize fp
(text object or binary file containing a JSON document) to a Python object. After reading your JSON file you need
json.load(fp)
to deserialize fp
(text object or binary file containing a JSON document) to a Python object.
import json
with open("results.json") as fp:
json_content =
json.load(fp)
for engine in json_content["data"]["reports"]["domain_blacklist"]["engines"]:
print(engine["detected"])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.