\" always have to be escaped in JSON?" />
开发者

Can you avoid Gson converting "<" and ">" into unicode escape sequences?

I noticed that Gson converts the string "<" into an unicode escape sequence in JSON output. Can you avoid this somehow, or do characters like "<" and ">" always have to be escaped in JSON?

开发者_开发百科

Consider this example which prints {"s":"\u003c"}; I'd want simply {"s":"<"}.

public static void main(String[] args) {
    Gson gson = new GsonBuilder().create();
    System.out.println(gson.toJson(new Foo()));  
}

static class Foo {
    String s = "<";
}

Context: the piece of JSON I'm creating has nothing to do with HTML pages or even JavaScript; it's just used to pass certain structured information to another piece of software (embedded in a device, written in C).


You need to disable HTML escaping.

Gson gson = new GsonBuilder().disableHtmlEscaping().create();


the Ampasand symbol was replacing with \u0026 , by using this it got resolved.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜