我有一个json结构,如下所示
{
"components":
{
"metadata":[
[
{"component":2},{"component":9}
],
[
{"component":10},{"component":15}
]
]
}
}
我的POJO课程是()
public class Components {
private List<Metadata> metadata;
// Getters and setters
}
public class Metadata{
private List<String> component;
// Getters and setters
}
我在ObjectMapper中收到错误如下:
"Can not deserialize instance of com.sections.metadata.Metadata out of START_ARRAY token"
任何人都可以帮我纠正我的POJO课程结构吗? 由于json是一个2D数组,我创建了两个arrayLists, 一个在Component类中,另一个在Metadata类中。
提前致谢!
答案 0 :(得分:0)
{
"components":
{
"metadata":[
[
{"component":2},{"component":9}
],
[
{"component":10},{"component":15}
]
]
}
}
上述JSON与您指定的对象结构不对应。
答案 1 :(得分:0)
这就是你需要的:
public class ComponentsWrapper {
private Components components;
public Components getComponents() {
return components;
}
public void setComponents(Components components) {
this.components = components;
}
public static void main(String[] args) throws IOException, URISyntaxException {
ObjectMapper objectMapper = new ObjectMapper();
ComponentsWrapper components =
objectMapper.readValue(<your JSON>),
ComponentsWrapper.class);
}
}
class Components {
private List<List<Component>> metadata;
public List<List<Component>> getMetadata() {
return metadata;
}
public void setMetadata(List<List<Component>> metadata) {
this.metadata = metadata;
}
}
class Component {
private int component;
public int getComponent() {
return component;
}
public void setComponent(int component) {
this.component = component;
}
}