Java:使用Jackson剖析json时如何精确获取节点中的值?

打印 上一主题 下一主题

主题 1540|帖子 1540|积分 4620

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
使用Jackson剖析json时,经常会必要获取到某一节点下的值,例如:
   {
    “data”: {
      "test1": "value1",
      "test2": null,
      "test3": 10
    }
  }
   以Jackson2.13.5为例,使用at(jsonPtrExp)这种API,剖析json的代码并获取节点值的方法:
  1. ObjectMapper objectMapper = new ObjectMapper();
  2. String content = "{"data": {"test1": "value1","test2": null,"test3": 10}}";
  3. JsonNode root = objectMapper.readTree(content);
  4. JsonNode node = root.at("/data/test1");
  5. node.asText();       # 1
  6. node.textValue();    # 2
复制代码
这里有个让人疑惑的地方,获取此中的值时,到底应该用#1,还是用#2。
我们写一个测试用例,来看看它们的区别:
  1. import java.io.IOException;
  2. import com.fasterxml.jackson.databind.JsonNode;
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. public class JacksonTester {
  5. public static void main(String[] args) throws IOException {
  6.         ObjectMapper objectMapper = new ObjectMapper();
  7.         String content = "{"data": {"test1": "value1","test2": null,"test3": 10}}";
  8.         JsonNode root = objectMapper.readTree(content);
  9.         {
  10.                 JsonNode node = root.at("/data/test1");
  11.                 System.out.println("node.isMissingNode()##" + node.isMissingNode());
  12.                 System.out.println("node.isNull()##" + node.isNull());
  13.                 System.out.println("node.asText() == null##" + (node.asText() == null));
  14.                 System.out.println("node.asText()##" + node.asText());
  15.                 System.out.println("node.textValue() == null##" + (node.textValue() == null));
  16.                 System.out.println("node.textValue()##" + node.textValue());
  17.                 System.out.println("-------------");
  18.         }
  19.         {
  20.                 JsonNode node = root.at("/data/test2");
  21.                 System.out.println("node.isMissingNode()##" + node.isMissingNode());
  22.                 System.out.println("node.isNull()##" + node.isNull());
  23.                 System.out.println("node.asText() == null##" + (node.asText() == null));
  24.                 System.out.println("node.asText()##" + node.asText());
  25.                 System.out.println("node.textValue() == null##" + (node.textValue() == null));
  26.                 System.out.println("node.textValue()##" + node.textValue());
  27.                 System.out.println("-------------");
  28.         }
  29.         {
  30.                 JsonNode node = root.at("/data/test3");
  31.                 System.out.println("node.isMissingNode()##" + node.isMissingNode());
  32.                 System.out.println("node.isNull()##" + node.isNull());
  33.                 System.out.println("node.asText() == null##" + (node.asText() == null));
  34.                 System.out.println("node.asText()##" + node.asText());
  35.                 System.out.println("node.textValue() == null##" + (node.textValue() == null));
  36.                 System.out.println("node.textValue()##" + node.textValue());
  37.                 System.out.println("-------------");
  38.         }
  39.         {
  40.                 JsonNode node = root.at("/data/test4");
  41.                 System.out.println("node.isMissingNode()##" + node.isMissingNode());
  42.                 System.out.println("node.isNull()##" + node.isNull());
  43.                 System.out.println("node.asText() == null##" + (node.asText() == null));
  44.                 System.out.println("node.asText()##" + node.asText());
  45.                 System.out.println("node.textValue() == null##" + (node.textValue() == null));
  46.                 System.out.println("node.textValue()##" + node.textValue());
  47.                 System.out.println("-------------");
  48.         }
  49.         }
  50. }
复制代码
执行结果:
   node.isMissingNode()##false
node.isNull()##false
node.asText() == null##false
node.asText()##value1
node.textValue() == null##false
node.textValue()##value1
-------------
node.isMissingNode()##false
node.isNull()##true
node.asText() == null##false
node.asText()##null
node.textValue() == null##true
node.textValue()##null
-------------
  node.isMissingNode()##false
node.isNull()##true
node.asText() == null##false
node.asText()##10
node.textValue() == null##true
node.textValue()##null
-------------
node.isMissingNode()##true
node.isNull()##false
node.asText() == null##false
node.asText()##
node.textValue() == null##true
node.textValue()##null
-------------
   从执行结果中可以看到
节点值为null时节点不存在时节点值为字符串时节点值不为字符串时
asText()“null”"""value1""10"
textValue()nullnull"value1"null
以下是JsonNode的子类及它们的条理结构

下面总结一下,当节点为上述范例并且值不为空时,asText()和textValue()获取到的值,以及精确获取值的方法
asText()textValue()获取值的方法
ArrayNode""null使用elements()遍历
ObjectNode""null使用elements()遍历
BinaryNode二进制的Base64编码后的值nullbinaryValue()
BooleanNode"true" / "false"nullbooeanValue()
MissingNode""null
NullNode"null"null直接赋值为null
NumberNode数字对应的字符串null bigIntegerValue()
decimal()
doubleValue()
intValue()
longValue()
POJONode内部包罗的对象toString()返回值nullgetPojo()
TextNode字符串值字符串值 textValue()
asText()
综上,如果想要获取到各种范例节点实际的值,必要先将它转换成对应的范例,再使用对应范例的相应方法。
但如果只是想获取到ValueNode及其子范例节点的字符串值,只必要做如下判断后,直接使用asText()即可
  1. if (!node.isMissingNode() && !node.isNull())
  2. {
  3.   String value = node.asText();
  4. }
复制代码
 如果是使用textValue() ,只要节点值的范例是字符串,不消判断isMissingNode()和node.isNull(),就可以直接使用。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

飞不高

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表