简体   繁体   中英

Jackson vs Simple Json Parser

Using Simple JSONParser for parsing string to JSONObject and there was no issue with it. Later hear of jackson parser which seems faster compared to Simple JSONParser. But the problem is if there is a jsonobject within jsonobject, after parsing through Jackson parser, unable to extract the inner jsonobject, which is not the case in Simple JSONParser and pretty much easier.

Eg: {"Key1":"Value1","Key2":{"innerJSonKey":"innerJSonValue"}} this is the jsonobject which is converted to String using toString().

JSON Simple

JSONParser jp = new JSONParser();

JSONObject jo = (JSONObject)jp.parse(jsonString);

JSONObject innerjson = (JSONObject)jo.get("innerJSonKey"); -- this pretty much works

JACKSON

ObjectMapper mapper = new ObjectMapper();

JSONObject jo = mapper.readValue(jsonString,JSONObject.class);

JSONObject innerjson = (JSONObject)jo.get("innerJSonKey"); -- **But this step is failing**

Please post your comments, whether am I doing any mistake or is there a solution

You can try the following code to get the value corresponding to innerJSonKey

ObjectMapper mapper = new ObjectMapper();
JsonNode rootNode = mapper.readValue(jsonString, JsonNode.class);
JsonNode innerNode = rootNode.get("Key2").get("innerJSonKey");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM