Problems with using Gson when parsing Unicode

Source: Internet
Author: User



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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.