{
"icon": "service",
"title": "A",
"type": "service",
"children": [
{
"icon": "sharedlibraries",
"title": "sharedlibraries",
"type": "sharedlibraries",
"children": [
{
"icon": "war",
"title": "abc.war ( ui-shared-lib )",
"path": "abc/common/Services/1.2.0/lib/comp.war",
"reference": >"abc/common/templates/applications/11.1.2.3.jar/config/config.xml",
"type": "sharedlibrary",
"version": "11.1.2.0@11.1.2.0",
"children": [
{
"icon": "jar",
"title": "comp1.jar",
"path": >"abc/common/SharedServices/1.2.0/lib/comp.war/WEB-INF/lib/comp.jar",
"reference": >"abc/common/Services/1.2.0/lib/comp.war/WEB-INF/lib",
"type": "jar",
"thirdpartyjar": "true"
}
]
},
:
:
:
}
我需要检索,属性"路径",所有节点的名称" children",其中" thirdpartyjar" atribute = true。这有可能使用杰克逊吗?
更新:我试过以下:
public void parse(File file) throws JsonProcessingException,
IOException {
ObjectMapper objectMapper = new ObjectMapper();
//JsonNode rootNode = objectMapper.readTree(file);
Model model = objectMapper.readValue(file, Model.class);
listThirdPartyJars(model);
while (true) {
Model children = model.getChildren();
if (!(children == null)) {
listThirdPartyJars(children);
model = children;
} else {
break;
}
}
}
public void listThirdPartyJars(Model model) {
boolean thirdPartyJars = model.isThirdPartyJar();
if (thirdPartyJars == true)
System.out.println(model.getPath());
}
但是,遇到以下异常: com.fasterxml.jackson.databind.JsonMappingException:无法从START_ARRAY令牌中反序列化com.manager.Model的实例 在[来源:D:\ my_json.json; line:4,column:22](通过参考链:com.manager.Model [" children"])
答案 0 :(得分:0)
是,这是可能的。你需要有一个模型,它将自己作为属性引用,有点像:
public class Model {
String icon;
String title;
String path;
String reference;
String type;
String version;
boolean thirdPartyJar;
Model children;
...getters, setters and overriden methods
}
您可以使用ObjectMapper
将Json字符串映射到此对象,以进一步访问属性为thirdPartyJar
的子项为真。
答案 1 :(得分:0)
考虑您在问题中发布的JSON(通过一些修复使其有效):
{
"icon": "service",
"title": "A",
"type": "service",
"children": [
{
"icon": "sharedlibraries",
"title": "sharedlibraries",
"type": "sharedlibraries",
"children": [
{
"icon": "war",
"title": "abc.war ( ui-shared-lib )",
"path": "abc/common/Services/1.2.0/lib/comp.war",
"reference": "abc/common/templates/applications/11.1.2.3.jar/config/config.xml",
"type": "sharedlibrary",
"version": "11.1.2.0@11.1.2.0",
"children": [
{
"icon": "jar",
"title": "comp1.jar",
"path": "abc/common/SharedServices/1.2.0/lib/comp.war/WEB-INF/lib/comp.jar",
"reference": "abc/common/Services/1.2.0/lib/comp.war/WEB-INF/lib",
"type": "jar",
"thirdpartyjar": "true"
}
]
}
]
}
]
}
使用Jayway JsonPath和$..children[?(@.thirdpartyjar=='true')].path
等查询可以解决问题,并会给您以下结果:
[
"abc/common/SharedServices/1.2.0/lib/comp.war/WEB-INF/lib/comp.jar"
]
您可以对其进行测试here。
要使用Jayway JsonPath,请将以下依赖项添加到项目中:
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.3.0</version>
</dependency>
然后阅读JSON文档:
String json = "...";
List<String> paths = JsonPath.read(json, "$..children[?(@.thirdpartyjar=='true')].path");
除Jayway JsonPath外,您可以将JSON映射到Java类(或简单地使用Jackson树模型),使用Jackson解析JSON,在树上递归迭代并提取所需的数据。