package com.example.demo1;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class Demo1ApplicationTests {
@Test
void contextLoads() {
JSONObject entries = new JSONObject(getStr());
String byPath = entries.getByPath("userList.list[1].name",String.class);
System.out.println(byPath);//json深层获取
String byPath1 = getJson().getByPath("userList.list[1].name", String.class);
System.out.println("byPath1 = " + byPath1);//xml转json深层获取
}
String getStr(){
return "{\"userList\":{\"list\":[{\"name\":\"张三\",\"age\":12},{\"name\":\"李四\",\"age\":12}]}}";
}
JSONObject getJson(){
String s = "<userList>\n" +
"\t<list>\n" +
"\t\t<name>张三</name>\n" +
"\t\t<age>12</age>\n" +
"\t</list>\n" +
"\t\t<list>\n" +
"\t\t<name>李四</name>\n" +
"\t\t<age>12</age>\n" +
"\t</list>\n" +
"</userList>";
JSONObject json = JSONUtil.parseFromXml(s);
return json;
}
}
11-07
1万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)