我正在使用IBM的Watson Visual Recognition API,并且试图格式化输出JSON数据以仅显示“类”,“分数”及其两个值。
我用过
print(json.dumps(classes_result, indent=2))
获取输出:
"images": [
{
"classifiers": [
{
"classifier_id": "default",
"name": "default",
"classes": [
{
"class": "honey buzzard",
"score": 0.639,
"type_hierarchy": "/animal/bird/bird of prey/hawk/honey buzzard"
},
{
"class": "hawk",
"score": 0.891
},
{
"class": "bird of prey",
"score": 0.918
},
答案 0 :(得分:0)
尝试一下,使用字典上的嵌套循环:
for image in images:
classifiers = image['classifiers']
for classifier in classifiers:
classes = classifier['classes']
for _class in classes:
class_value = _class['class']
score_value = _class['score']
print(class_value, score_value)