On Thu, Jun 17, 2021 at 9:29 AM Никита <[email protected]> wrote:
>
> I need to parse json using jackson:
>
> String string = "{\"field\" : \" \\/ \"}";
>
> JsonNode node = new ObjectMapper().readTree(string);
>  String result = node.toString();
>  System.out.println(result);
>
> I expect this kind of output:
>
> result == {"field":" \/ "}
>
> But, I end up with:
>
> result == {"field":" / "}
>
> How can I receive output like this?
>
> result == {"field":" \/ "}
>
> java

This seems to be working as expected: Java compiler replaces "\\" in
input String with single "\", and then in JSON String "\" is taken to
mean JSON escaping so "\/" is decoded as "/".
It might make sense to construct JsonNode value instead of raw JSON
escaped String since then all escaping would be handled by Jackson --
but if you must pass a String that contains JSON, you will need to use
more escaping.
Something like:

String string = "{\"field\" : \" \\\\/ \"}";

(two "\"s for Javac, and then two of those for JSON escaping)

Constructing JsonNode on the other hand would be

ObjectNode n = mapper.createObjectNode();
n.put("field", "\\/"); // still need doubling for javac

-+ Tatu +-

-- 
You received this message because you are subscribed to the Google Groups 
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jackson-user/CAL4a10j77gO7zbXg6et4q_o4uzh%3DQYJB8CE%3DPrY1jcqTEKjoXQ%40mail.gmail.com.

Reply via email to