使用翻新功能解析JSON的模型结构

时间:2018-08-08 07:37:00

标签: android json retrofit

我具有以下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数组,但是当我想解析时,它为空。

5 个答案:

答案 0 :(得分:0)

我认为您尝试将元素数组放入单个对象“私有元素”中

答案 1 :(得分:0)

以此更改您的pojo。您还可以添加所需的各自的构造函数。

 public class Element {

@SerializedName("id")
@Expose
private Integer id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("type")
@Expose
private String type;
@SerializedName("constraints")
@Expose
private String constraints;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getConstraints() {
return constraints;
}

public void setConstraints(String constraints) {
this.constraints = constraints;
}

}

public class Product {

@SerializedName("id")
@Expose
private Integer id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("elements")
@Expose
private List<Element> elements = null;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public List<Element> getElements() {
return elements;
}

public void setElements(List<Element> elements) {
this.elements = elements;
}

}

答案 2 :(得分:0)

在json中,您具有Element列表,在模型中,您将Element声明为Object

将此private Element elements;更改为元素列表

答案 3 :(得分:0)

尝试此pojo或您可以从此link

中进行制作
  private String id;

private String name;

private ArrayList<Elements> elements;

public String getId ()
{
    return id;
}

public void setId (String id)
{
    this.id = id;
}

public String getName ()
{
    return name;
}

public void setName (String name)
{
    this.name = name;
}

public ArrayList<Elements> getElements ()
{
    return elements;
}

public void setElements (ArrayList<Elements> elements)
{
    this.elements = elements;
}

@Override
public String toString()
{
    return "ClassPojo [id = "+id+", name = "+name+", elements = "+elements+"]";
}

public class Elements
{
private String id;

private String name;

private String constraints;

private String type;

public String getId ()
{
    return id;
}

public void setId (String id)
{
    this.id = id;
}

public String getName ()
{
    return name;
}

public void setName (String name)
{
    this.name = name;
}

public String getConstraints ()
{
    return constraints;
}

public void setConstraints (String constraints)
{
    this.constraints = constraints;
}

public String getType ()
{
    return type;
}

public void setType (String type)
{
    this.type = type;
}

@Override
public String toString()
{
    return "ClassPojo [id = "+id+", name = "+name+", constraints = "+constraints+", 
type = "+type+"]";
}
}

答案 4 :(得分:0)

您需要像这样修改您的POJO:

Element.java

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Element {

@SerializedName("id")
@Expose
private Integer id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("type")
@Expose
private String type;
@SerializedName("constraints")
@Expose
private String constraints;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getConstraints() {
return constraints;
}

public void setConstraints(String constraints) {
this.constraints = constraints;
}

}

Product.java

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Product {

@SerializedName("id")
@Expose
private Integer id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("elements")
@Expose
private List<Element> elements = null;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public List<Element> getElements() {
return elements;
}

public void setElements(List<Element> elements) {
this.elements = elements;
}

}

P.S:您应该使用“ http://www.jsonschema2pojo.org/”网站从提供给它的JSON字符串自动生成POJO。

我希望这会有所帮助。