当前位置: 首页> 教育> 大学 > 今日大连疫情最新消息_上海人才中心网站_百度高级搜索首页_刷神马关键字排名软件

今日大连疫情最新消息_上海人才中心网站_百度高级搜索首页_刷神马关键字排名软件

时间:2025/7/11 20:29:41来源:https://blog.csdn.net/netyeaxi/article/details/143691997 浏览次数:1次
今日大连疫情最新消息_上海人才中心网站_百度高级搜索首页_刷神马关键字排名软件

使用Jackson时解析json时,经常会使用jsonPath直接获取某一节点下的值,这种方式非常直观 ,例如:

{

  “data”: {

    "test1": "value1",

    "test2": null,

    "test3": 10

  }

}

 以Jackson2.13.5,使用at(jsonPtrExp)这种API,解析json的代码:

ObjectMapper objectMapper = new ObjectMapper();String content = "{"data": {\"test1\": \"value1\",\"test2\": null,\"test3\": 10}}";JsonNode root = objectMapper.readTree(content);JsonNode node = root.at("/data/test1");node.asText();       # 1
node.textValue();    # 2

这里有个让人疑惑的地方,获取其中的值时,到底应该用#1还是#2。

我们写一个测试用例,来看看它们的区别:

import java.io.IOException;import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;public class JacksonTester {public static void main(String[] args) throws IOException {ObjectMapper objectMapper = new ObjectMapper();String content = "{"data": {\"test1\": \"value1\",\"test2\": null,\"test3\": 10}}";JsonNode root = objectMapper.readTree(content);{JsonNode node = root.at("/data/test1");System.out.println("node.isMissingNode()##" + node.isMissingNode());System.out.println("node.isNull()##" + node.isNull());System.out.println("node.asText() == null##" + (node.asText() == null));System.out.println("node.asText()##" + node.asText());System.out.println("node.textValue() == null##" + (node.textValue() == null));System.out.println("node.textValue()##" + node.textValue());System.out.println("-------------");}{JsonNode node = root.at("/data/test2");System.out.println("node.isMissingNode()##" + node.isMissingNode());System.out.println("node.isNull()##" + node.isNull());System.out.println("node.asText() == null##" + (node.asText() == null));System.out.println("node.asText()##" + node.asText());System.out.println("node.textValue() == null##" + (node.textValue() == null));System.out.println("node.textValue()##" + node.textValue());System.out.println("-------------");}{JsonNode node = root.at("/data/test3");System.out.println("node.isMissingNode()##" + node.isMissingNode());System.out.println("node.isNull()##" + node.isNull());System.out.println("node.asText() == null##" + (node.asText() == null));System.out.println("node.asText()##" + node.asText());System.out.println("node.textValue() == null##" + (node.textValue() == null));System.out.println("node.textValue()##" + node.textValue());System.out.println("-------------");}{JsonNode node = root.at("/data/test4");System.out.println("node.isMissingNode()##" + node.isMissingNode());System.out.println("node.isNull()##" + node.isNull());System.out.println("node.asText() == null##" + (node.asText() == null));System.out.println("node.asText()##" + node.asText());System.out.println("node.textValue() == null##" + (node.textValue() == null));System.out.println("node.textValue()##" + node.textValue());System.out.println("-------------");}}}

执行结果:

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

从结果上看,如果要使用asText() ,应该是要先做如下判断

if (!node.isMissingNode() && !node.isNull())
{String value = node.asText();
}

如果是使用textValue() ,只要节点值的类型是字符串,不用判断isMissingNode()和node.isNull(),就可以直接使用。

关键字:今日大连疫情最新消息_上海人才中心网站_百度高级搜索首页_刷神马关键字排名软件

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: