Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Faster XML Jackson: Remove double quotes

Tags:

java

json

jackson

I have the following json:

{"test":"example"} 

I use the following code from Faster XML Jackson.

JsonParser jp = factory.createParser("{\"test\":\"example\"}"); json = mapper.readTree(jp); System.out.println(json.get("test").toString()); 

It outputs:

"example" 

Is there a setting in Jackson to remove the double quotes?

like image 547
basickarl Avatar asked Feb 21 '15 13:02

basickarl


People also ask

How to remove double quotation?

Use the String. replaceAll() method to remove all double quotes from a string, e.g. str. replaceAll('"', '') . The replace() method will return a new string with all double quotes removed.

How to remove double quotes from a String?

To remove double quotes just from the beginning and end of the String, we can use a more specific regular expression: String result = input. replaceAll("^\"|\"$", ""); After executing this example, occurrences of double quotes at the beginning or at end of the String will be replaced by empty strings.


2 Answers

Well, what you obtain when you .get("test") is a JsonNode and it happens to be a TextNode; when you .toString() it, it will return the string representation of that TextNode, which is why you obtain that result.

What you want is to:

.get("test").textValue(); 

which will return the actual content of the JSON String itself (with everything unescaped and so on).

Note that this will return null if the JsonNode is not a TextNode.

like image 105
fge Avatar answered Oct 11 '22 21:10

fge


Simple generic ternary to use the non-quoted text, otherwise keep the node intact.

node.isTextual() ? node.asText() : node 
like image 25
Jon Polaski Avatar answered Oct 11 '22 21:10

Jon Polaski