Tech Guy
Tech Guy

Reputation: 447

How to parse json string with UTF-8 characters using java?

I have a json string with SUBSTITUTE () utf-8 character. I'm getting parsing exception when I try to convert json string to java object using jackson. Can you please let me know how to encode and decode utf-8 characters ?

ObjectMapper mapper = new ObjectMapper();
mapper.readValue(jsonString, MY_DOMAIN_OBJECT.class);

jsonString:

{"studentId":"753253-2274", "information":[{"key":"1","value":"Get alerts on your phone(SUBSTITUTE character is present here. Unable to paste it)To subscribe"}]}

enter image description here

Error:

Illegal unquoted character ((CTRL-CHAR, code 26)): has to be escaped using backslash to be included in string value

Upvotes: 2

Views: 7013

Answers (1)

Madplay
Madplay

Reputation: 1037

Can you try this?

ObjectMapper mapper = new ObjectMapper();
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
mapper.readValue(jsonString, MY_DOMAIN_OBJECT.class);

I hope it helps you: Javadoc

Feature that determines whether parser will allow JSON Strings to contain unquoted control characters (ASCII characters with value less than 32, including tab and line feed characters) or not. If feature is set false, an exception is thrown if such a character is encountered. Since JSON specification requires quoting for all control characters, this is a non-standard feature, and as such disabled by default.

Upvotes: 4

Related Questions