我有以下JSON数据。
{
"faceDetails": [
{
"boundingBox": {
"width": 0.36888888,
"height": 0.2777778,
"left": 0.4814815,
"top": 0.4422222
},
"emotions": [
{
"type": "SAD",
"confidence": 40.245743
},
{
"type": "CONFUSED",
"confidence": 15.142041
},
{
"type": "SURPRISED",
"confidence": 1.9677103
}
],
"smile": {
"value": false,
"confidence": 90.49947
}
},
{
"boundingBox": {
"width": 0.36888888,
"height": 0.2777778,
"left": 0.4814815,
"top": 0.4422222
},
"emotions": [
{
"type": "SAD",
"confidence": 40.245743
},
{
"type": "CONFUSED",
"confidence": 15.142041
},
{
"type": "SURPRISED",
"confidence": 1.9677103
}
],
"smile": {
"value": false,
"confidence": 90.49947
}
},
{
"boundingBox": {
"width": 0.36888888,
"height": 0.2777778,
"left": 0.4814815,
"top": 0.4422222
},
"emotions": [
{
"type": "SAD",
"confidence": 40.245743
},
{
"type": "CONFUSED",
"confidence": 15.142041
},
{
"type": "SURPRISED",
"confidence": 1.9677103
}
],
"smile": {
"value": false,
"confidence": 90.49947
}
},
{
"boundingBox": {
"width": 0.36888888,
"height": 0.2777778,
"left": 0.4814815,
"top": 0.4422222
},
"emotions": [
{
"type": "Happy",
"confidence": 40.245743
},
{
"type": "CONFUSED",
"confidence": 15.142041
},
{
"type": "SURPRISED",
"confidence": 1.9677103
}
],
"smile": {
"value": false,
"confidence": 90.49947
}
}
]
}
根据这些数据,我需要获得具有最高置信度分数的情绪,创建变量并将此分数分配给该分数。对于前者从上面的数据来看,我的输出应该如下所示。
Happy: 1
Sad: 3
这里的打印顺序无关紧要。我问你这个因为,我无法理解如何在旅途中创建一个变量。即如果另一个变量type
为cool
,那么我需要一个名为cool
的变量及其计数。
此外,我无法理解如何捕获emotions
值
以下是我尝试的代码
private static void getTheResultBasedOnEmotions(String inputText)
throws JsonParseException, JsonMappingException, IOException {
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> map = mapper.readValue(inputText, new TypeReference<Map<String, Object>>() {
});
List faceCount = (List) map.get("faceDetails");
System.out.println(faceCount.toString());
}
我得到的结果如下
[
{
"boundingBox": {
"width": 0.36888888,
"height": 0.2777778,
"left": 0.4814815,
"top": 0.4422222
},
"emotions": [
{
"type": "SAD",
"confidence": 40.245743
},
{
"type": "CONFUSED",
"confidence": 15.142041
},
{
"type": "SURPRISED",
"confidence": 1.9677103
}
],
"smile": {
"value": false,
"confidence": 90.49947
}
},
{
"boundingBox": {
"width": 0.36888888,
"height": 0.2777778,
"left": 0.4814815,
"top": 0.4422222
},
"emotions": [
{
"type": "SAD",
"confidence": 40.245743
},
{
"type": "CONFUSED",
"confidence": 15.142041
},
{
"type": "SURPRISED",
"confidence": 1.9677103
}
],
"smile": {
"value": false,
"confidence": 90.49947
}
},
{
"boundingBox": {
"width": 0.36888888,
"height": 0.2777778,
"left": 0.4814815,
"top": 0.4422222
},
"emotions": [
{
"type": "SAD",
"confidence": 40.245743
},
{
"type": "CONFUSED",
"confidence": 15.142041
},
{
"type": "SURPRISED",
"confidence": 1.9677103
}
],
"smile": {
"value": false,
"confidence": 90.49947
}
},
{
"boundingBox": {
"width": 0.36888888,
"height": 0.2777778,
"left": 0.4814815,
"top": 0.4422222
},
"emotions": [
{
"type": "Happy",
"confidence": 40.245743
},
{
"type": "CONFUSED",
"confidence": 15.142041
},
{
"type": "SURPRISED",
"confidence": 1.9677103
}
],
"smile": {
"value": false,
"confidence": 90.49947
}
}
]
请让我知道我该怎么做。
这里我使用jackson文件进行解析。
由于
答案 0 :(得分:0)
这是我的方法:
借助Gson和Online POJO generator
将JSON解析为POJOPS。您可以使用任何库,例如jackson,将JSON解析为POJO。
//classes generated by Json2POJO generator
class FaceDetail {
private Object boundingBox;
private List<Emotion> emotions;
private Object smile;
public List<Emotion> getEmotions() {
return emotions;
}
}
class Emotion {
private String type;
private Double confidence;
public String getType() {
return type;
}
}
//Parse Json to POJO
List<FaceDetail> faceDetails = gson.fromJson(JSONData, ArrayList<FaceDetail>.class);
按类型计算情感数字
//Flap map all emotion to an ArrayList
List<Emotion> allEmotions = faceDetails.stream()
.map(FaceDetail::getEmotions)
.flatMap(emotions -> emotions.stream())
.collect(Collectors.toList());
//count
long countSAD = allEmotions.stream()
.filter(emotion -> emotion.getType().equals("SAD"))
.count();
希望这有助于:)