Reputation: 472
How do I use the YAJL lib from Scott Klement correctly to read the response of Watson Visual Recognition? The json object looks like this:
{
"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
}
Now I want get the values from the classes objects with class, score_hierarchy. How can I get to the classes array? After getting the images object i cannot find the 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);
Upvotes: 0
Views: 97
Reputation: 472
Here's the code that works now:
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;
Upvotes: 0
Reputation: 1605
I believe this should do it:
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);
And if you want more examples, this page provides some more: https://www.fieldexit.com/forum/display?threadid=199
Upvotes: 1