我有这个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
}
]
}
]
}
]
}
我想提取"音调" in" document_tone" - > " tone_categories",意思是我想提取愤怒,厌恶,恐惧,喜悦和悲伤的分数并打印出来。文件中显示的第一个。
输出应该很简单:
0.380012, 0.093832, 0.156902, 0.204329, 0.243196
我已经制作了这段代码:
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);
但没有成功。我怀疑一个接一个地获取嵌套对象有什么问题。
答案 0 :(得分:3)
tone_categories
不是对象而是数组:
JSONArray tone_categories = (JSONArray) document_tone.get("tone_categories");
与tones
相同。
答案 1 :(得分:0)
tone_categories
和tones
属于JsonArray类型。我们可以使用get方法在特定位置获取JsonElement或使用for循环迭代数组元素。