如何格式化JSON以显示“ Class”,“ Score”及其值?

时间:2019-08-26 14:50:38

标签: python json

我正在使用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
            },

1 个答案:

答案 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)