【发布时间】:2018-08-08 07:36:59
【问题描述】:
我有以下 JSON 结构:
[
{
"id":1,
"name":"car",
"elements":[
{
"id":1,
"name":"price",
"type":"textField",
"constraints":"blablabla"
},
{
"id":2,
"name":"color",
"type":"textField",
"constraints":"blablabla"
},
{
"id":3,
"name":"images",
"type":"image",
"constraints":"blablabla"
}
]
}
]
我有以下型号:
public class Product {
private Long id;
private String name;
@Expose
private Element elements;
public Product(Long id, String name, Element elements) {
this.id = id;
this.name = name;
this.elements = elements;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public Element getElements() {
return elements;
}
}
public class Element {
private Long id;
private String name;
private String type;
private String constraints;
public Element(Long id, String name, String constraints, String type) {
this.id = id;
this.type=type;
this.name = name;
this.constraints = constraints;
}
public String getType() {
return type;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public String getConstraints() {
return constraints;
}
}
Elements 模型有问题,我收到以下错误: 错误:java.lang.IllegalStateException:应为 BEGIN_OBJECT,但在第 1 行第 35 列路径 $[0].elements 处为 BEGIN_ARRAY
如何更改模型以使其正常工作?我试图将 Product 类中的元素更改为 JSONObject 数组,但是当我想解析时,它是空的。
【问题讨论】: