简体   繁体   English

杰克逊vs简单杰森解析器

[英]Jackson vs Simple Json Parser

Using Simple JSONParser for parsing string to JSONObject and there was no issue with it. 使用Simple JSONParser将字符串解析为JSONObject,并且没有问题。 Later hear of jackson parser which seems faster compared to Simple JSONParser. 后来听到杰克逊解析器的声音,它比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. 但是问题是,在通过Jackson解析器进行解析之后,如果jsonobject中存在一个jsonobject,则无法提取内部jsonobject,这在Simple JSONParser中不是这种情况,并且更加容易。

Eg: {"Key1":"Value1","Key2":{"innerJSonKey":"innerJSonValue"}} this is the jsonobject which is converted to String using toString(). 例如:{“ Key1”:“ Value1”,“ Key2”:{“ innerJSonKey”:“ innerJSonValue”}}}这是使用toString()转换为String的jsonobject。

JSON Simple JSON简单

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 您可以尝试以下代码来获取与innerJSonKey对应的值

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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