在JSON文件中提取嵌套值[重复]

时间:2022-12-01 00:54:26

This question already has an answer here:

这个问题在这里已有答案:

I have this JSON file

我有这个JSON文件

{
  "document_tone": {
    "tone_categories": [
      {
        "category_id": "emotion_tone",
        "category_name": "Emotion Tone",
        "tones": [
          {
            "tone_id": "anger",
            "tone_name": "Anger",
            "score": 0.380012
          },
          {
            "tone_id": "disgust",
            "tone_name": "Disgust",
            "score": 0.093832
          },
          {
            "tone_id": "fear",
            "tone_name": "Fear",
            "score": 0.156902
          },
          {
            "tone_id": "joy",
            "tone_name": "Joy",
            "score": 0.204329
          },
          {
            "tone_id": "sadness",
            "tone_name": "Sadness",
            "score": 0.243196
          }
        ]
      },
      {
        "category_id": "language_tone",
        "category_name": "Language Tone",
        "tones": [
          {
            "tone_id": "analytical",
            "tone_name": "Analytical",
            "score": 0.978091
          },
          {
            "tone_id": "confident",
            "tone_name": "Confident",
            "score": 0.0
          },
          {
            "tone_id": "tentative",
            "tone_name": "Tentative",
            "score": 0.0
          }
        ]
      },
      {
        "category_id": "social_tone",
        "category_name": "Social Tone",
        "tones": [
          {
            "tone_id": "openness_big5",
            "tone_name": "Openness",
            "score": 0.453176
          },
          {
            "tone_id": "conscientiousness_big5",
            "tone_name": "Conscientiousness",
            "score": 0.707102
          },
          {
            "tone_id": "extraversion_big5",
            "tone_name": "Extraversion",
            "score": 0.856905
          },
          {
            "tone_id": "agreeableness_big5",
            "tone_name": "Agreeableness",
            "score": 0.793529
          },
          {
            "tone_id": "emotional_range_big5",
            "tone_name": "Emotional Range",
            "score": 0.835602
          }
        ]
      }
    ]
  },
  "sentences_tone": [
    {
      "sentence_id": 0,
      "input_from": 0,
      "input_to": 48,
      "text": "SVG transform on text attribute works excellent!",
      "tone_categories": [
        {
          "category_id": "emotion_tone",
          "category_name": "Emotion Tone",
          "tones": [
            {
              "tone_id": "anger",
              "tone_name": "Anger",
              "score": 0.262452
            },
            {
              "tone_id": "disgust",
              "tone_name": "Disgust",
              "score": 0.031795
            },
            {
              "tone_id": "fear",
              "tone_name": "Fear",
              "score": 0.104458
            },
            {
              "tone_id": "joy",
              "tone_name": "Joy",
              "score": 0.555167
            },
            {
              "tone_id": "sadness",
              "tone_name": "Sadness",
              "score": 0.103881
            }
          ]
        },
        {
          "category_id": "language_tone",
          "category_name": "Language Tone",
          "tones": [
            {
              "tone_id": "analytical",
              "tone_name": "Analytical",
              "score": 0.801827
            },
            {
              "tone_id": "confident",
              "tone_name": "Confident",
              "score": 0.0
            },
            {
              "tone_id": "tentative",
              "tone_name": "Tentative",
              "score": 0.0
            }
          ]
        },
        {
          "category_id": "social_tone",
          "category_name": "Social Tone",
          "tones": [
            {
              "tone_id": "openness_big5",
              "tone_name": "Openness",
              "score": 0.285863
            },
            {
              "tone_id": "conscientiousness_big5",
              "tone_name": "Conscientiousness",
              "score": 0.441621
            },
            {
              "tone_id": "extraversion_big5",
              "tone_name": "Extraversion",
              "score": 0.710666
            },
            {
              "tone_id": "agreeableness_big5",
              "tone_name": "Agreeableness",
              "score": 0.521599
            },
            {
              "tone_id": "emotional_range_big5",
              "tone_name": "Emotional Range",
              "score": 0.35773
            }
          ]
        }
      ]
    },
    {
      "sentence_id": 1,
      "input_from": 49,
      "input_to": 114,
      "text": "This snippet for example will increase your text by 2x at Y-axis.",
      "tone_categories": [
        {
          "category_id": "emotion_tone",
          "category_name": "Emotion Tone",
          "tones": [
            {
              "tone_id": "anger",
              "tone_name": "Anger",
              "score": 0.278447
            },
            {
              "tone_id": "disgust",
              "tone_name": "Disgust",
              "score": 0.137868
            },
            {
              "tone_id": "fear",
              "tone_name": "Fear",
              "score": 0.177609
            },
            {
              "tone_id": "joy",
              "tone_name": "Joy",
              "score": 0.052633
            },
            {
              "tone_id": "sadness",
              "tone_name": "Sadness",
              "score": 0.426658
            }
          ]
        },
        {
          "category_id": "language_tone",
          "category_name": "Language Tone",
          "tones": [
            {
              "tone_id": "analytical",
              "tone_name": "Analytical",
              "score": 0.932977
            },
            {
              "tone_id": "confident",
              "tone_name": "Confident",
              "score": 0.0
            },
            {
              "tone_id": "tentative",
              "tone_name": "Tentative",
              "score": 0.0
            }
          ]
        },
        {
          "category_id": "social_tone",
          "category_name": "Social Tone",
          "tones": [
            {
              "tone_id": "openness_big5",
              "tone_name": "Openness",
              "score": 0.609711
            },
            {
              "tone_id": "conscientiousness_big5",
              "tone_name": "Conscientiousness",
              "score": 0.872715
            },
            {
              "tone_id": "extraversion_big5",
              "tone_name": "Extraversion",
              "score": 0.881295
            },
            {
              "tone_id": "agreeableness_big5",
              "tone_name": "Agreeableness",
              "score": 0.881589
            },
            {
              "tone_id": "emotional_range_big5",
              "tone_name": "Emotional Range",
              "score": 0.938061
            }
          ]
        }
      ]
    }
  ]
}

I want to extract the "tones" in "document_tone" -> "tone_categories", meaning I want to extract the scores of anger, disgust, fear, joy and sadness and print them out. The first ones shown in the file.

我想提取“document_tone” - >“tone_categories”中的“音调”,这意味着我想提取愤怒,厌恶,恐惧,喜悦和悲伤的分数并打印出来。文件中显示的第一个。

The output should be this simple:

输出应该很简单:

0.380012, 0.093832, 0.156902, 0.204329, 0.243196

I've made this code:

我做了这个代码:

JSONParser parser = new JSONParser();
JSONObject jsonObject = (JSONObject) parser.parse(new FileReader("myfile.json"));
JSONObject document_tone = (JSONObject) jsonObject.get("document_tone"); 

JSONObject tone_categories = (JSONObject) document_tone.get("tone_categories");

JSONObject tones = (JSONObject) tone_categories.get("tones");

double joy = (double) tones.get("joy");
double sadness = (double) tones.get("sadness");
double disgust = (double) tones.get("disgust");
double anger = (double) tones.get("anger");
double fear = (double) tones.get("fear");

System.out.println(joy + " ;" + sadness + " ;" + disgust + " ;" + anger + " ;" + fear);

But had no success. I suspect there is something wrong in getting the nested objects one after the other.

但没有成功。我怀疑一个接一个地获取嵌套对象有什么问题。

2 个解决方案

#1


3  

tone_categories is not an object but an array:

tone_categories不是一个对象而是一个数组:

JSONArray tone_categories = (JSONArray) document_tone.get("tone_categories");

The same with tones.

音调一样。

#2


0  

The tone_categories and tones are of type JsonArray. We can use get method to get JsonElement at a particular position or a for loop to iterate over the array elements.

tone_categories和音调属于JsonArray类型。我们可以使用get方法在特定位置获取JsonElement或使用for循环迭代数组元素。

#1


3  

tone_categories is not an object but an array:

tone_categories不是一个对象而是一个数组:

JSONArray tone_categories = (JSONArray) document_tone.get("tone_categories");

The same with tones.

音调一样。

#2


0  

The tone_categories and tones are of type JsonArray. We can use get method to get JsonElement at a particular position or a for loop to iterate over the array elements.

tone_categories和音调属于JsonArray类型。我们可以使用get方法在特定位置获取JsonElement或使用for循环迭代数组元素。