【发布时间】:2017-07-03 23:58:57
【问题描述】:
我有一个由函数创建的 json 文件。
文件如下所示:
{
"images": [
{
"image": "/WATSON/VISUAL-REC/../IMAGES/OBAMA.jpg",
"classifiers": [
{
"classes": [
{
"score": 0.921,
"class": "President of the United States",
"type_hierarchy": "/person/President of the United States"
},
{
"score": 0.921,
"class": "person"
},
{
"score": 0.73,
"class": "ivory color"
},
{
"score": 0.648,
"class": "Indian red color"
}
],
"classifier_id": "default",
"name": "default"
}
]
}
],
"custom_classes": 0,
"images_processed": 1
}
我写了一个函数来检查值“person”是否存在。 如果存在值“人”,我将增加
#LOADING MY JSON FILE
output_file = open(new_json_file).read()
output_json = json.loads(output_file)
#SETTING PERSON VALUE TO 0
person = 0
#CONDITIONS
if "person" in output_json["images"][0]["classifiers"][0]["classes"][0]["class"] :
person=1
print (' FUNCTION : jsonanalysis // step There is a person in this image')
return person
else :
person = 0
print (' FUNCTION : jsonanalysis // step There is a NO person in this image')
return person
我猜这不是检查 JSON 键中是否返回值“person”的正确方法,因为它没有找到该值。我在这里错过了什么?
我研究了一些类似的线程并尝试调整我的功能:
使用 Python 从 JSON 文件中解析值?
在python的嵌套json字典中查找值
但我的问题是“类”键。关键“类”(如“象牙色”)有几个值,而不仅仅是“人”。
【问题讨论】:
-
@roganjosh:好的