简体   繁体   English

杰克逊:无法从 JsonNode 获取字符串值

[英]Jackson: can not get String value from JsonNode

I have a root-JsonNode我有一个根 JsonNode

JsonNode payloadNode;

with the following textValue (log.warn("PAYLOAD_NODE" + payloadNode.textValue());):使用以下 textValue (log.warn("PAYLOAD_NODE" + payloadNode.textValue());):

 {"id":0,"uid":""}

But when I,m trying to get String-value from this node:但是当我试图从这个节点获取字符串值时:

 JsonNode idNode = payloadNode.get("id");

I receive null我收到空

Have a look at this .看看这个

Method to use for accessing String values.用于访问字符串值的方法。 Does NOT do any conversions for non-String value nodes;不对非字符串值节点做任何转换; for non-String values (ones for which isTextual() returns false) null will be returned.对于非字符串值(isTextual() 返回 false 的值)将返回 null。 For String values, null is never returned (but empty Strings may be)对于 String 值,从不返回 null(但可能返回空字符串)

As it is a text value it is just a string that has no field "id".因为它是一个文本值,所以它只是一个没有字段“id”的字符串。

So if you have something like this:所以如果你有这样的事情:

String s = "{\"id\":0,\"uid\":\"\"}";
payloadNode = om.valueToTree(s);

you would get such a log output if your JsonNode was just a string as in my example.如果你的 JsonNode 只是一个字符串,就像我的例子一样,你会得到这样的日志输出。 You need to read your possible string as a json tree so like:您需要将可能的字符串读取为 json 树,例如:

payloadNode = om.readTree(s);

Doing this will give you "0" for id and null for textValue() .这样做会给你 id 的“0”和textValue() null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM