在JSON文件中查找特定值 [英] Looking for a specific value in JSON file

查看:155
本文介绍了在JSON文件中查找特定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由函数创建的json文件。

I have a json file created by a function.

该文件如下所示:

    {
  "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
}

我写了一个要检查的函数是一个价值人存在。
如果值人存在,我将增加

I wrote a function that is going to check is a the value "person" is present. If the value "person" is present, I will increment with

#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,因为它找不到该值。我在这里错过了什么?

I guess This is not the right way to check if the value "person" is returned in the JSON Key since it does not find the value. What Am I missing here ?

我调查了一些这样的线程并试图调整我的功能:

I looked into some threads like those and tried to adapt my function:

使用Python从JSON文件解析值?

Parsing values from a JSON file using Python?

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

Find a value within nested json dictionary in python

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

But my issue is the "class" key. There is several values for the key "class" ( like "ivory color") not just "person".

推荐答案

自从person类不一定是classes数组的第一个元素中的第一个。你应该迭代那里的所有元素。

Since the "person" class is not necessarily the first in the first element of the "classes" array. you should iterate over all of the elements there.

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

如果你想检查任何图像/分类器中的类条目中是否有'person',你还需要迭代这些数组:

if you want to check if there is 'person' in a class entry in ANY of the images/classifiers, you will need to iterate over those arrays as well:

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

注意,如果你想计算'人'出现的总数,你不应该在循环内返回,但只有在完成之后。

Note, that if you want to count the total number of 'person' occurrences, you should not return inside the loop, but only after it is completed.

这篇关于在JSON文件中查找特定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆