Search code examples
javajsonjacksonjson-simple

json-simple versus jackson for parsing json when do not have obj type


To convert json String to pojo using jackson API can use :

String jsonInString = "{\"age\":33,\"messages\":[\"msg 1\",\"msg 2\"],\"name\":\"mkyong\"}";
User user1 = mapper.readValue(jsonInString, User.class);

This requires that create class User that matches structure of json String.

Using json-simple API can use instead :

JSONObject json = (JSONObject)new JSONParser().parse(jsonInString);

Using json-simple do not need to include a pojo that matches json format. Can similar be used in jackson ? json-simple is less verbose as do not have to create the class that matches json structure.


Solution

  • You can use similar API

    JsonNode node = mapper.readTree(jsonInString);