简体   繁体   中英

GWT JSON Parse is removing \ character

In GWT, I am trying to provide a regex as an input through JSON and attempting to read it back again. But on reading back the value from JSON, the \\ character that I give during input is missed.

String input = "{\"digit\":\"(\d*)\"}";

JSONValue  parse   = JSONParser.parseLeneient(input);
JSONObject jsonObj = parse.isObject();

if(jsonObj!=null) {
 for(String key:jsonObj.keySet()) {
    System.out.prinltn(jsonObj.get(key).toString();
 }
}

The output that I get is

(d*)

How do I make the \\ character to appear as well in my ouput?

The JSON specification states

The representation of strings is similar to conventions used in the C family of programming languages. A string begins and ends with quotation marks. All Unicode characters may be placed within the quotation marks except for the characters that must be escaped: quotation mark, reverse solidus, and the control characters (U+0000 through U+001F).

A reverse solidus is \\ . So you can't place a \\ alone in a JSON string.

Since your parsing is lenient it just ignores the \\ , since \\d is not a valid escape sequence.

Basically the JSON

{
    "digit": "(\d*)"
}

is not valid .

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