如何优雅的解析多层嵌套的复制JSON数据集
**JsonUtils**```
package org.example.wenhao.json;
import com.fasterxml.jackson.databind.ObjectMapper;
import ognl.Ognl;
import ognl.OgnlContext;
import java.util.Map;
import java.util.logging.Logger;
public class JsonUtils {
private static Logger logger = Logger.getLogger("JsonUtils");
private final static ObjectMapper objectMapper = new ObjectMapper();
public static ObjectMapper getInstance() {
return objectMapper;
}
public static <T> T parse(String jsonStr, Class<T> clazz) throws Exception {
return getInstance().readValue(jsonStr, clazz);
}
/**
* 使用ognl对象图导航语言
* @Param map
* @param path
* @param klass
* @return
* @param <T>
* @throws Exception
*/
public static <T> T getValue(Map map, String path, Class<T> klass) throws Exception {
OgnlContext ctx = new OgnlContext();
ctx.setRoot(map);
T value = (T) Ognl.getValue(path, ctx, ctx.getRoot());
return value;
}
}
```
**TestJson**
```
package org.example.wenhao.json;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;
/**
*
*如何优雅的解析多层嵌套的复制JSON数据集。
*
*json数据
*
* {
* "a1": {
* "b1": {
* "c1": {
* "d1": "d1"
* }
* }
* },
* "a2": {
* "b2": {
* "c2": {
* "d2": ["i1", "i2", "i3"]
* }
* }
* }
* }
*/
public class TestJson {
private static Logger logger = Logger.getLogger("TestJson");
@Test
public void test(){
String jsonData = "{\"a1\":{\"b1\":{\"c1\":{\"d1\":\"d1\"}}},\"a2\":{\"b2\":{\"c2\":{\"d2\":[\"i1\",\"i2\",\"i3\"]}}}}";
try {
//Map<String,Object> map = JsonUtils.getValue(jsonData, "a1.bi.ci", Map.class);
//parse the json data into a map
Map<String, Object> map = JsonUtils.parse(jsonData, Map.class);
Map<String,Object> map1 = (Map<String, Object>) JsonUtils.getValue(map, "a1.b1", Object.class);
System.out.println("map1 = " + map1);
//get the value of the key "a1.b1.c1.d1" from the map
String s = JsonUtils.getValue(map, "a1.b1.c1.d1", String.class);
System.out.println(s);
Map<String,Object> map2 = (Map<String, Object>) JsonUtils.getValue(map, "a2.b2.c2", Object.class);
System.out.println("map2 = " + map2);
//get the value of the key "a2.b2.c2.d2" from the map
List<String> list = JsonUtils.getValue(map, "a2.b2.c2.d2", List.class);
System.out.println(list);
} catch (Exception e) {
logger.info(e.getMessage());
//throw new RuntimeException(e);
} finally {
System.out.println("this");
}
}
}
```
Maven
```
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>jsondemo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>jsondemo</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.16</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.11.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.11.4</version>
</dependency>
<dependency>
<groupId>ognl</groupId>
<artifactId>ognl</artifactId>
<version>3.1.26</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
</dependencies>
</project>
``` 学习了学习了 楼主几年工作经验了 jsonPath也非常好用 虽然每没有看懂,还是送上感谢。JSON有格式化工具吧 不错,看上去有用 quicktype,了解一下? quicktype挺好用的 sunflash 发表于 2023-12-13 08:50
虽然每没有看懂,还是送上感谢。JSON有格式化工具吧
https://www.bejson.com/ wh52pj 发表于 2023-12-15 18:34
https://www.bejson.com/
感谢感谢
页:
[1]