你能避免Gson转换“<”和“>”。到unicode转义序列? [英] Can you avoid Gson converting "<" and ">" into unicode escape sequences?
问题描述
我注意到 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转换“<”和“>”。到unicode转义序列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!