2017-01-09 64 views
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 

這裏的打印順序並不重要。我問你這是因爲我無法理解如何在旅途中創建變量。即如果有另一個變量typecool,那麼我需要一個名爲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文件來解析。

感謝

+2

我不想寫完整的答案與代碼,但我可以給你一些提示。嘗試遍歷'faceDetails'數組,然後從其「情緒」中選擇正確的值。要存儲它,你可以使用'HashMap',將情緒作爲一個鍵並且將其作爲一個值來計數。 –

+0

使用鍵值技術。使用鍵獲取值並對其進行計數。我在gson上工作。只是分享我的想法 – atiqkhaled

回答

0

這裏是我的方法:

解析JSON成POJO與GSON的幫助和Online POJO generator

PS.You可以使用任何庫,像傑克遜,解析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); 

計數情感號與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(); 

希望能幫助:)