Java 如何计算json中发生的事件数
我有下面的JSON数据Java 如何计算json中发生的事件数,java,json,jackson,Java,Json,Jackson,我有下面的JSON数据 { "faceDetails": [ { "boundingBox": { "width": 0.36888888, "height": 0.2777778, "left": 0.4814815, "top": 0.4422222 }, "emotio
{
"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
在这里,印刷顺序无关紧要。我问你这个问题是因为,我无法理解如何在运行中创建变量。i、 e.如果有另一个变量类型
为cool
,那么我需要一个名为cool
的变量及其计数
此外,我无法理解如何捕捉情绪
值
下面是我尝试的代码
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());
}
请让我知道我该怎么做
这里我使用jackson文件进行解析
谢谢以下是我的方法:
在Gson和
另外,您可以使用任何库,如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);
//由Json2POJO生成器生成的类
类FaceDetail{
私有对象边界框;
私人情感;
私人物品微笑;
公共列表{
回归情感;
}
}
阶级情感{
私有字符串类型;
私人双重信任;
公共字符串getType(){
返回类型;
}
}
//将Json解析为POJO
List faceDetails=gson.fromJson(JSONData,ArrayList.class);
按类型使用
//将所有情感映射到ArrayList
List=faceDetails.stream()
.map(FaceDetail::GetMootations)
.flatMap(情绪->情绪.stream())
.collect(Collectors.toList());
//计数
long countSAD=allEmotions.stream()
.filter(情绪->情绪.getType().equals(“SAD”))
.count();
希望这能有所帮助:)我不想用代码写完整的答案,但我可以给你一些提示。尝试迭代
faceDetails
数组,然后从其emotions
中选择正确的值。要存储它,您可以使用HashMap
,将情感作为键,将计数作为值。使用键值技术。使用键获取值并计数。我在gson工作。分享我的想法
//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();