java 更快的 XML Jackson:删除双引号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28646572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 13:56:34  来源:igfitidea点击:

Faster XML Hymanson: Remove double quotes

javajsonHymanson

提问by Karl Morrison

I have the following json:

我有以下 json:

{"test":"example"}

I use the following code from Faster XML Hymanson.

我使用来自 Faster XML Hymanson 的以下代码。

JsonParser jp = factory.createParser("{\"test\":\"example\"}");
json = mapper.readTree(jp);
System.out.println(json.get("test").toString());

It outputs:

它输出:

"example"

Is there a setting in Hymanson to remove the double quotes?

Hymanson 中是否有删除双引号的设置?

回答by fge

Well, what you obtain when you .get("test")is a JsonNodeand it happens to be a TextNode; when you .toString()it, it will return the string representation of that TextNode, which is why you obtain that result.

好吧,当你.get("test")是 a时你得到了JsonNode什么,而它恰好是 a TextNode; 当您使用.toString()它时,它将返回 that 的字符串表示形式TextNode,这就是您获得该结果的原因。

What you want is to:

你想要的是:

.get("test").textValue();

which will return the actual content of the JSON String itself (with everything unescaped and so on).

这将返回 JSON 字符串本身的实际内容(所有内容都未转义等等)。

Note that this will return null if the JsonNodeis nota TextNode.

请注意,这将返回NULL,如果JsonNode不是一个TextNode

回答by Jon Polaski

Simple generic ternary to use the non-quoted text, otherwise keep the node intact.

使用非引用文本的简单通用三元,否则保持节点完整。

node.isTextual() ? node.asText() : node