利用jackson的JsonNode封装工具类快速获取json字符串中任意节点的json数据

关联资源

实现效果

工具类实现 xxx.xxx.xxx的形式直接从json字符串中获取任意节点字段的值,如下图所示:
在这里插入图片描述

实现代码

当前引入的jackson的依赖为:

<dependency>
	 <groupId>com.fasterxml.jackson.core</groupId>
	 <artifactId>jackson-databind</artifactId>
	 <version>2.10.1</version>
</dependency>

封装Json处理工具类

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @author lzp
 * @Date:2023/3/1
 * @description: json处理工具类
 */
public class MyJsonUtils {

	/**
	 * 单例objectMapper,提高性能
	 * 网上的性能测试:https://blog.csdn.net/qq_31960623/article/details/117778291
	 */
	private static final ObjectMapper objectMapper = new ObjectMapper();

	/**
	 * 单例 饿汉式
	 */
	private static final MyJsonUtils INSTANCE = new MyJsonUtils();

	private MyJsonUtils() {
	}

	/**
	 * 单例模式,暴露一个单例的工具类实例获取
	 */
	public static MyJsonUtils getInstance() {
		return INSTANCE;
	}

	/**
	 * 通过key路径取到最后一级key对应的jsonNode
	 *
	 * @param jsonStr 原始的json字符串
	 * @param keyPath xx.xxx.xxx格式的key路径
	 * @return
	 */
	public JsonNode getValueByKeyPath(String jsonStr, String keyPath) throws JsonProcessingException {
		JsonNode jsonNode = objectMapper.readTree(jsonStr);
		String[] paths = keyPath.split("\\.");

		// 遍历key路径,直到最后一层的key
		JsonNode currentNode = jsonNode;
		for (String key : paths) {
			currentNode = currentNode.get(key);
			if (currentNode == null) {
				return null;
			}
		}
		return currentNode;
	}

	/**
	 * 通过key路径取到最后一级key对应的value值
	 *
	 * @param jsonStr 原始json字符串
	 * @param keyPath xx.xxx.xxx格式的key路径
	 * @param cls     值的对象类型
	 */
	public <T> T getValueByKeyPath(String jsonStr, String keyPath, Class<T> cls) throws JsonProcessingException {
		JsonNode jsonNode = this.getValueByKeyPath(jsonStr, keyPath);
		if (jsonNode == null) {
			return null;
		}
		return objectMapper.treeToValue(jsonNode, cls);
	}

	/**
	 * 测试
	 */
	public static void main(String[] args) throws JsonProcessingException {
		String jsonStr = "{\"head\":{\"face\":\"隔壁老王的小脸\",\"eye\":{\"left\":\"轮回眼\",\"right\":\"写轮眼\"},\"iq\":250},\"body\":\"身材修长\",\"likeStudy\":true}";
		// 分别取到各个类型的值
		String valueStr = MyJsonUtils.getInstance().getValueByKeyPath(jsonStr, "head.eye.left", String.class);
		Integer valueInt = MyJsonUtils.getInstance().getValueByKeyPath(jsonStr, "head.iq", Integer.class);
		Boolean valueBool = MyJsonUtils.getInstance().getValueByKeyPath(jsonStr, "likeStudy", Boolean.class);
		System.out.println(valueStr);
		System.out.println(valueInt);
		System.out.println(valueBool);
	}

}


实现思路梳理

关键代码就下面这块

  • 先把字符串按 "."进行分割成数组
  • 然后遍历key,注意每次循环的时候,currentNode = 下一个key的值,类似递归的思想,最后一次循环中的key就能取到最后一层

在这里插入图片描述

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值