简体   繁体   中英

How to count the number of occurances in a json

I've the below JSON data.

{
    "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
            }
        }
    ]
}

From this data I need to get the emotion with the top confidence score, create a variable and assign this score to that. For ex. from the above data, My output should be like below.

Happy: 1
Sad: 3

Here the order of printing doesn't matter. I'm asking you this because, I'm unable to understand on how to create a variable on the go. ie If there is another variable the type is cool , then I need a variable named cool and its count.

Also I'm unable to understand how to capture the emotions value

Below is the code that I tried

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());
    }

and the result that I get is as below

[
{
            "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
            }
        }
]

Please let me know how can I do this.

Here I use jackson file to parse.

Thanks

Here's my approach:

Parse JSON into POJO with help of Gson and Online POJO generator

PS.You can use any library, like jackson, to parse JSON to 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);

Count Emotion number by type with Java stream API

//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();

Hope this could help:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM