0

I have the following JSON structure:

[  
   {  
      "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"
         }
      ]
   }
]

And I have the following models:

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;
    }
}

The Elements model is which I have problems, I am getting the following error: ERROR: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 35 path $[0].elements

How can I change the model to make it work? I tried to change the elements in Product class to JSONObject array but when I wanted to parse, it was empty.

Legend
  • 31
  • 1
  • 6

5 Answers5

0

I think you try to put an array of Elements into "private Element elements" which is a single object

Ltei
  • 425
  • 1
  • 6
  • 14
0

Change your pojo with this. Also you can add your respective constructor which you require.

 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;
}

}
zephyr
  • 665
  • 6
  • 19
0

in json you have list of Element and in the model you declared Element as an Object

change this private Element elements; to List of Element

Yassine BELDI
  • 562
  • 5
  • 16
0

Try this pojo or you can make from this 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+"]";
}
}
mehul chauhan
  • 1,792
  • 11
  • 26
0

You need to modify your POJOs like this:

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: You should use 'http://www.jsonschema2pojo.org/' website for generating POJO automatically from the JSON string you provide to it.

I hope this helps.

Salman Khakwani
  • 6,684
  • 7
  • 33
  • 58