2017-02-15 64 views
0

我有一个由函数创建的json文件。在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 
} 

我写的是要检查的功能是值“人”的存在。 如果值“人”的存在,我将与

#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键返回,因为它没有发现的价值的正确方法。我在这里错过了什么?

我看着像一些线​​程,并试图适应我的功能:使用Python中的JSON文件

解析值?

查找嵌套JSON字典中的值在python

但我的问题是“类”的关键。关键的“班级”(如“象牙色”)不仅有“人”,还有几个值。

+0

@ roganjosh:好的 – MouIdri

回答

1

由于“人”类不是必需的第一个在“classes”数组的第一个元素中。你应该遍历所有的元素。

如果要检查是否有“人”在任何图像/分类的类条目
for entry in in output_json['images'][0]['classifiers'][0]['classes']: 
    if ('class' in entry) and (entry['class'] == 'person'): 
     person += 1 
     print (' FUNCTION : jsonanalysis // step There is a person in this image') 
     return person 

,你将需要遍历这些阵列,以及:

for image_entry in output_json['images']: 
    for classifier_entry in image_entry['classifiers']: 
     for class_entry in in classifier_entry["classes"]: 
      if class_entry['class'] == 'person': 
       person += 1 
       print (' FUNCTION : jsonanalysis // step There is a person in this image') 
       return person 

注,如果要计算“人物”出现次数的总数,则不应在循环内部返回,而必须在完成后才返回。

+0

我试过了,它工作的很好! – MouIdri

0

尝试以下操作:

1让我们假设你的数据存储到Python字典,如果不尝试将其转换。

data_dict = { 
    "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 
} 
  • 使用下列转换的JSON来字符串: 进口JSON

    data_str = json.dumps(data_dict) 
    
  • 现在检查该串中的字(data_str):

    if 'person' in data_str: 
         return True 
    else: 
         return False  
    
  • +0

    这种方法会产生很多不好的结果。例如,即使存在字符串'xxxpersonxxx'或'person'是一个键等,它也会匹配。 – MByD

    +0

    更改格式是我原来的解决方法。但我想继续浏览json。 – MouIdri

    +1

    数据已经是'json'了,这可能只是一个更大文件的片段,所以这可能对OP可能想要做的测试类型有很多误报。 – roganjosh