你能避免Gson转换“<”和“>”。到unicode转义序列? [英] Can you avoid Gson converting "<" and ">" into unicode escape sequences?

查看:144
本文介绍了你能避免Gson转换“<”和“>”。到unicode转义序列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到 Gson 将字符串<转换为JSON输出中的Unicode转义序列。你能以某种方式避免这种情况,或者做一些像<的字符和>总是必须在JSON中转义?



考虑这个例子打印 {s:\\\<} ;我只想要 {s:<}

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

static class Foo {
String s =<;
}

上下文:我创建的JSON片断与HTML无关网页甚至JavaScript;它只是用来将某些结构化信息传递给另一个软件(嵌入在设备中,用C语言编写)。 需要停用HTML转义

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


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();

这篇关于你能避免Gson转换“&lt;”和“&gt;”。到unicode转义序列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆