Before the use of Gson parsing is not recorded, so today to do a small summary,
For example, when encountering Unicode characters like this "\u003d", we want to decode this character and use Gson to express it.
Gson gson=new Gson();
String s2 = "\"{\\\"hi\\\":\\\"\\u003d\\\"}\"";
System.out.println("s2:"+s2);
String s = gson.fromJson(s2, String.class);
System.out.println(s);
The above output is
S2: "{\" hi\ ": \" \u003d\ "}"
{"HI": "="}
It is visible that the string passed into the JSON must be an escaped character, and the format must be correct, otherwise an error will occur.
Similarly, decoding a Unicode character can also use Urldecoder, which is convenient for direct parsing of Unicode.
The code is as follows:
String ss = URLDecoder.decode("\u003d","utf-8");
System.out.println(ss);
This method is much more convenient than the previous one.
In addition, special characters are automatically encoded when the Gson is converted to a JSON string, and if you want to ignore this feature, use the
New Gsonbuilder (). disablehtmlescaping (). Create ()
You can create a Gson object.
Problems with using Gson when parsing Unicode