How to Convert JSON string to Map using Jackson

  Apr 2, 2019

I have basic idea of how Jackson converts to and from an json to Java object, i was wondering how to do a map conversion directly from json to java.

Solved 0
2 months ago 1 Answer 0 views

Answers ( 1 )

  1. 2 months ago

    Here is a good example:

    try {
    
                ObjectMapper mapper = new ObjectMapper();
                String json = "{\"name\":\"tbc\", \"age\":20}";
    
                Map<String, Object> map = new HashMap<String, Object>();
    
                // convert JSON string to Map
                map = mapper.readValue(json, new TypeReference<Map<String, String>>(){});
    
                System.out.println(map);
    
            } catch (JsonGenerationException e) {
                e.printStackTrace();
            } catch (JsonMappingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    

*Avoid using any other website url of links , otherwise your answer may not be visible on website until verification is complete.

Your Answer