Convert JSON to Map

后端 未结 17 2893
天涯浪人
天涯浪人 2020-11-22 10:20

What is the best way to convert a JSON code as this:

{ 
    \"data\" : 
    { 
        \"field1\" : \"value1\", 
        \"field2\" : \"value2\"
    }
}
         


        
17条回答
  •  死守一世寂寞
    2020-11-22 10:55

    Try this code:

      public static Map convertJsonIntoMap(String jsonFile) {
            Map map = new HashMap<>();
            try {
                ObjectMapper mapper = new ObjectMapper();
                mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
                mapper.readValue(jsonFile, new TypeReference>() {
                });
                map = mapper.readValue(jsonFile, new TypeReference>() {
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
            return map;
        }
    

提交回复
热议问题