我正在尝试从json文件中获取一系列对象,我有一个问题。
path.get("wgcTournaments.items")
我应该使用哪种路径在项目中获取所有项目(item0,item1,item2 ...)?
您能给我一个建议。
JSON示例
{
"wgcTournaments": {
"items": {
"jcr:primaryType": "nt:unstructured",
"item0": {
"jcr:primaryType": "nt:unstructured",
"test": "test",
"test1": "test1"
},
"item1": {
"jcr:primaryType": "nt:unstructured",
"test": "test",
"test1": "test1"
},
"item2": {
"jcr:primaryType": "nt:unstructured",
"test": "test",
"test1": "test1"
},
"item3": {
"jcr:primaryType": "nt:unstructured",
"test": "test",
"test1": "test1"
}
}
}
}
从项目对象过滤项目的最佳方法,但我不明白如何使用JSON路径进行操作。
最后,我找到了解决问题的解决方案。
如果您想从项目中获取物品,则需要使用此JSON路径
path.getObject("wgcTournaments.items*.
find{it.key.startsWith('item')}.value",ItemClass[].class);
注意:它被重新确定,他使用了更多的详细信息,您可以在这里找到http://docs.groovy-lang.org/latest/html/documentation/#_gpath
您正在尝试将对象置于对象数组中。您的代码或JSON(很可能)是错误的。
如果您想将items
作为数组进行挑选,则您的JSON应该是以下内容:
{
"wgcTournaments": {
"items": [
{
"jcr:primaryType": "nt:unstructured",
"item0": {},
"item1": {},
"item2": {},
"item3": {}
}
]
}
}
否则,如果您的JSON正确,则应使用以下行进行JSON启用:
path.getObject("wgcTournaments.items", MyClass.class)
编辑:编辑后,这似乎是您想要的:
如果您的JSON正确并且您确实想要一个数组,我认为每个itemX
都是键,{}
是相应的值。在这种情况下,您必须知道,您不能在JSON中具有关联数组,因此您应该使用自定义解决方案进行对象化,因为您的关联数组将转换为对象。