0
我有下面的JSON數據。如何統計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文件來解析。
感謝
我不想寫完整的答案與代碼,但我可以給你一些提示。嘗試遍歷'faceDetails'數組,然後從其「情緒」中選擇正確的值。要存儲它,你可以使用'HashMap',將情緒作爲一個鍵並且將其作爲一個值來計數。 –
使用鍵值技術。使用鍵獲取值並對其進行計數。我在gson上工作。只是分享我的想法 – atiqkhaled