1 回答
TA贡献1810条经验 获得超4个赞
您的JSON有效负载中有两个数组。所以,你需要POJO为它创建额外的。请参阅以下示例:
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.util.List;
public class JsonApp {
public static void main(String[] args) throws Exception {
File jsonFile = new File("./resource/test.json").getAbsoluteFile();
ObjectMapper mapper = new ObjectMapper();
Root root = mapper.readValue(jsonFile, Root.class);
System.out.println(root);
}
}
class Root {
private Class1 class1;
//getters, setters, toString
}
class Class1 {
private String prop1;
private List<NestedProps> prop2;
//getters, setters, toString
}
class NestedProps {
private List<NestedProp> nestedProp;
//getters, setters, toString
}
class NestedProp {
private String p1;
private String p2;
//getters, setters, toString
}
对于以下JSON:
{
"class1": {
"prop1": "pp",
"prop2": [
{
"nestedProp": [
{
"p1": "127",
"p2": "1"
},
{
"p1": "128",
"p2": "2"
}
]
}
]
}
}
以上示例打印:
Root{class1=Class1{prop1='pp', prop2=[NestedProps{nestedProp=[NestedProp{p1='127', p2='1'}, NestedProp{p1='128', p2='2'}]}]}}
添加回答
举报