近来项目中有个比对报文的需求。
因为是做迁移项目,原报文是 PHP
调用的,现报文是Java调用的,需要比对关键字段是否一致。这就涉及到解析Json
。因为要精确到某个字段的路径、值,就得去处理路径与值的映射关系。
本文的核心内容是,将 Json
字符串转换为树形节点,然后遍历树形节点,得到叶子节点对应的路径和值。
使用到的Json
依赖是:
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.14.2</version>
</dependency>
package org.example;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* Json 树练习
*
* @version V1.0
* @author: fengjinsong
* @date: 2023年02月28日 08时55分
*/
public class JsonTreeDemo {
public static void main(String[] args) throws JsonProcessingException {
String json = "{\n" +
" \"code\": 0,\n" +
" \"message\": null,\n" +
" \"data\": {\n" +
" \"newsList\": [\n" +
" {\n" +
" \"date\": \"2023-02-28\",\n" +
" \"title\": \"扎克伯格:Meta将组建顶级AI团队,专注生成式人\",\n" +
" \"source\": \"网易科技\",\n" +
" \"picUrl\": \"https://nimg.ws.126.net/?url=http://dingyue.ws.126.net/2023/0228/f4c03754j00rqrmir000sc0009c007ac.jpg&thumbnail=200y140&quality=100&type=jpg\",\n" +
" \"url\": \"https://www.163.com/dy/article/HULCT1KC0534A4SC.html\"\n" +
" },\n" +
" {\n" +
" \"date\": \"2023-02-28\",\n" +
" \"title\": \"结合最新ChatGPT的AI能力,「中科睿智XR」希望\",\n" +
" \"source\": \"网易科技\",\n" +
" \"picUrl\": \"https://nimg.ws.126.net/?url=http://dingyue.ws.126.net/2023/0228/72075d0bj00rqrmrj002qc0009c005uc.jpg&thumbnail=200y140&quality=100&type=jpg\",\n" +
" \"url\": \"https://www.163.com/dy/article/HULEE6FG05118DFD.html\"\n" +
" },\n" +
" {\n" +
" \"date\": \"2023-02-28\",\n" +
" \"title\": \"Meta组建顶级AI团队,将生成式人工智能应用到旗\",\n" +
" \"source\": \"网易科技\",\n" +
" \"picUrl\": \"https://nimg.ws.126.net/?url=http://dingyue.ws.126.net/2023/0228/94c72877j00rqroo8000cc0009c005uc.jpg&thumbnail=200y140&quality=100&type=jpg\",\n" +
" \"url\": \"https://www.163.com/dy/article/HULF8HCR0514R9P4.html\"\n" +
" },\n" +
" {\n" +
" \"date\": \"2023-02-28\",\n" +
" \"title\": \"5G 3年等于4G 5年 第一波运营商已取得商业成功\",\n" +
" \"source\": \"网易科技\",\n" +
" \"picUrl\": \"https://nimg.ws.126.net/?url=http://cms-bucket.ws.126.net/2023/0228/cc35354ap00rqrpf80018c0009c0070c.png&thumbnail=200y140&quality=100&type=jpg\",\n" +
" \"url\": \"https://www.163.com/tech/article/HULG6HIN00097U7S.html\"\n" +
" },\n" +
" {\n" +
" \"date\": \"2023-02-28\",\n" +
" \"title\": \"网易厦门数字产业中心将发力产业元宇宙\",\n" +
" \"source\": \"网易科技\",\n" +
" \"picUrl\": \"https://nimg.ws.126.net/?url=http://bjnewsrec-cv.ws.126.net/little363102f859aj00rqqcmc002qc000sg00izg.jpg&thumbnail=200y140&quality=100&type=jpg\",\n" +
" \"url\": \"https://www.163.com/dy/article/HULBOUSB0514R9NP.html\"\n" +
" }\n" +
" ],\n" +
" \"errorMsg\": null\n" +
" },\n" +
" \"success\": true\n" +
"}";
JsonTreeDemo demo = new JsonTreeDemo();
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(json);
Map<String, Object> resultMap = new LinkedHashMap<>(16);
demo.traverseJsonTreeLeafNode(resultMap, "", jsonNode);
resultMap.forEach((path, value) -> System.out.println("path => " + path + "\t\t, value => " + value));
}
/**
* 遍历Json树形节点,获取其叶子节点的路径
*
* @param map 结过存储(key是节点路径,value是节点对应的值)
* @param path 节点路径
* @param jsonNode json节点,对应的是一个json串
*/
void traverseJsonTreeLeafNode(Map<String, Object> map, String path, JsonNode jsonNode) {
// 值节点
if (jsonNode.isValueNode()) {
map.put(path, String.valueOf(jsonNode));
return;
}
// 对象节点
if (jsonNode.isObject()) {
jsonNode.fields().forEachRemaining(entry -> {
traverseJsonTreeLeafNode(map, path + "/" + entry.getKey(), entry.getValue());
});
}
// 数组节点
if (jsonNode.isArray()) {
int i = 0;
for (JsonNode node : jsonNode) {
traverseJsonTreeLeafNode(map, path + "[" + i++ + "]", node);
}
}
}
}
{
"code": 0,
"message": null,
"data": {
"newsList": [
{
"date": "2023-02-28",
"title": "扎克伯格:Meta将组建顶级AI团队,专注生成式人",
"source": "网易科技",
"picUrl": "https://nimg.ws.126.net/?url=http://dingyue.ws.126.net/2023/0228/f4c03754j00rqrmir000sc0009c007ac.jpg&thumbnail=200y140&quality=100&type=jpg",
"url": "https://www.163.com/dy/article/HULCT1KC0534A4SC.html"
},
{
"date": "2023-02-28",
"title": "结合最新ChatGPT的AI能力,「中科睿智XR」希望",
"source": "网易科技",
"picUrl": "https://nimg.ws.126.net/?url=http://dingyue.ws.126.net/2023/0228/72075d0bj00rqrmrj002qc0009c005uc.jpg&thumbnail=200y140&quality=100&type=jpg",
"url": "https://www.163.com/dy/article/HULEE6FG05118DFD.html"
},
{
"date": "2023-02-28",
"title": "Meta组建顶级AI团队,将生成式人工智能应用到旗",
"source": "网易科技",
"picUrl": "https://nimg.ws.126.net/?url=http://dingyue.ws.126.net/2023/0228/94c72877j00rqroo8000cc0009c005uc.jpg&thumbnail=200y140&quality=100&type=jpg",
"url": "https://www.163.com/dy/article/HULF8HCR0514R9P4.html"
},
{
"date": "2023-02-28",
"title": "5G 3年等于4G 5年 第一波运营商已取得商业成功",
"source": "网易科技",
"picUrl": "https://nimg.ws.126.net/?url=http://cms-bucket.ws.126.net/2023/0228/cc35354ap00rqrpf80018c0009c0070c.png&thumbnail=200y140&quality=100&type=jpg",
"url": "https://www.163.com/tech/article/HULG6HIN00097U7S.html"
},
{
"date": "2023-02-28",
"title": "网易厦门数字产业中心将发力产业元宇宙",
"source": "网易科技",
"picUrl": "https://nimg.ws.126.net/?url=http://bjnewsrec-cv.ws.126.net/little363102f859aj00rqqcmc002qc000sg00izg.jpg&thumbnail=200y140&quality=100&type=jpg",
"url": "https://www.163.com/dy/article/HULBOUSB0514R9NP.html"
}
],
"errorMsg": null
},
"success": true
}
path => /code , value => 0
path => /message , value => null
path => /data/newsList[0]/date , value => "2023-02-28"
path => /data/newsList[0]/title , value => "扎克伯格:Meta将组建顶级AI团队,专注生成式人"
path => /data/newsList[0]/source , value => "网易科技"
path => /data/newsList[0]/picUrl , value => "https://nimg.ws.126.net/?url=http://dingyue.ws.126.net/2023/0228/f4c03754j00rqrmir000sc0009c007ac.jpg&thumbnail=200y140&quality=100&type=jpg"
path => /data/newsList[0]/url , value => "https://www.163.com/dy/article/HULCT1KC0534A4SC.html"
path => /data/newsList[1]/date , value => "2023-02-28"
path => /data/newsList[1]/title , value => "结合最新ChatGPT的AI能力,「中科睿智XR」希望"
path => /data/newsList[1]/source , value => "网易科技"
path => /data/newsList[1]/picUrl , value => "https://nimg.ws.126.net/?url=http://dingyue.ws.126.net/2023/0228/72075d0bj00rqrmrj002qc0009c005uc.jpg&thumbnail=200y140&quality=100&type=jpg"
path => /data/newsList[1]/url , value => "https://www.163.com/dy/article/HULEE6FG05118DFD.html"
path => /data/newsList[2]/date , value => "2023-02-28"
path => /data/newsList[2]/title , value => "Meta组建顶级AI团队,将生成式人工智能应用到旗"
path => /data/newsList[2]/source , value => "网易科技"
path => /data/newsList[2]/picUrl , value => "https://nimg.ws.126.net/?url=http://dingyue.ws.126.net/2023/0228/94c72877j00rqroo8000cc0009c005uc.jpg&thumbnail=200y140&quality=100&type=jpg"
path => /data/newsList[2]/url , value => "https://www.163.com/dy/article/HULF8HCR0514R9P4.html"
path => /data/newsList[3]/date , value => "2023-02-28"
path => /data/newsList[3]/title , value => "5G 3年等于4G 5年 第一波运营商已取得商业成功"
path => /data/newsList[3]/source , value => "网易科技"
path => /data/newsList[3]/picUrl , value => "https://nimg.ws.126.net/?url=http://cms-bucket.ws.126.net/2023/0228/cc35354ap00rqrpf80018c0009c0070c.png&thumbnail=200y140&quality=100&type=jpg"
path => /data/newsList[3]/url , value => "https://www.163.com/tech/article/HULG6HIN00097U7S.html"
path => /data/newsList[4]/date , value => "2023-02-28"
path => /data/newsList[4]/title , value => "网易厦门数字产业中心将发力产业元宇宙"
path => /data/newsList[4]/source , value => "网易科技"
path => /data/newsList[4]/picUrl , value => "https://nimg.ws.126.net/?url=http://bjnewsrec-cv.ws.126.net/little363102f859aj00rqqcmc002qc000sg00izg.jpg&thumbnail=200y140&quality=100&type=jpg"
path => /data/newsList[4]/url , value => "https://www.163.com/dy/article/HULBOUSB0514R9NP.html"
path => /data/errorMsg , value => null
path => /success , value => true