如何使用 YAJL 库读取 Watson Visual Recognition JSON Respsonse?

How to read Watson Visual Recognition JSON Respsonse with YAJL Library?

如何正确使用 Scott Klement 的 YAJL 库来读取 Watson Visual Recognition 的响应? json 对象如下所示:

{
"images": [
    {
        "classifiers": [
            {
                "classifier_id": "default",
                "name": "default",
                "classes": [
                    {
                        "class": "outside mirror",
                        "score": 0.85,
                        "type_hierarchy": "/reflector/mirror/outside mirror"
                    },
                    {
                        "class": "mirror",
                        "score": 0.998
                    },
                    {
                        "class": "reflector",
                        "score": 0.998
                    },
                    {
                        "class": "car mirror",
                        "score": 0.764,
                        "type_hierarchy": "/reflector/mirror/car mirror"
                    },
                    {
                        "class": "rearview mirror",
                        "score": 0.714,
                        "type_hierarchy": "/reflector/mirror/rearview mirror"
                    },
                    {
                        "class": "ash grey color",
                        "score": 0.778
                    },
                    {
                        "class": "bottle green color",
                        "score": 0.532
                    }
                ]
            }
        ],
        "source_url": "https://.jpg",
        "resolved_url": "https://.jpg"
    }
],
"images_processed": 1,
"custom_classes": 0
}

现在我想使用 class、score_hierarchy 从 classes 对象中获取值。 我怎样才能到达 classes 数组? 获取图像对象后,我找不到 classifiers to continue...

docNode = yajl_stmf_load_tree( temporaryFile: errMsg);
  if errMsg <> '';
    return imageClasses;
  endif;

  i = 0;

  images = YAJL_OBJECT_FIND(docNode: 'images');

  dow yajl_array_loop(images: i: node);

    // TODO: How to continue to get "classifiers" object?

    j = 0;
    dow yajl_object_loop(node:j:key:val);

      select;
      when key = 'classes';
        imageClasses(i).class = yajl_get_string(val);
      when key = 'score';
        imageClasses(i).score = yajl_get_number(val);
      when key = 'type_hierarchy';
        imageClasses(i).typeHierarchy = yajl_get_string(val);
      endsl;

    enddo;

  enddo;

  yajl_tree_free(docNode);

我认为应该这样做:

 docNode = yajl_stmf_load_tree( temporaryFile: errMsg);
  if errMsg <> '';
    return imageClasses;
  endif;

  i = 0;

  images = YAJL_OBJECT_FIND(docNode: 'images');

  dow yajl_array_loop(images: i: node);

    classifiers = YAJL_OBJECT_FIND(node: 'classifiers');

    j = 0;
    dow yajl_object_loop(classifiers:j:key:val);

      select;
      when key = 'classes';
        imageClasses(i).class = yajl_get_string(val);
      when key = 'score';
        imageClasses(i).score = yajl_get_number(val);
      when key = 'type_hierarchy';
        imageClasses(i).typeHierarchy = yajl_get_string(val);
      endsl;

    enddo;

  enddo;

  yajl_tree_free(docNode);

如果您需要更多示例,此页面提供了更多示例: https://www.fieldexit.com/forum/display?threadid=199

这是现在可以使用的代码:

images = YAJL_OBJECT_FIND(docNode: 'images'); 
i = 0;

dow yajl_array_loop(images: i: node);

  classifiers = YAJL_OBJECT_FIND(node: 'classifiers');

  k = 0;
  dow yajl_array_loop(classifiers: k: node);

    classes = YAJL_OBJECT_FIND(node: 'classes');

    j = 0;
    dow yajl_array_loop(classes: j: node);

      val = YAJL_object_find(node:'class');
      imageClasses.classes(j).class = yajl_get_string(val);

      val = YAJL_object_find(node:'score');
      imageClasses.classes(j).score = yajl_get_number(val);

      val = YAJL_object_find(node:'type_hierarchy');
      imageClasses.classes(j).typeHierarchy = yajl_get_string(val);

  enddo;

 enddo;

enddo;