Chandra Eskay
Chandra Eskay

Reputation: 2203

Parse JSON to java Class

I have this JSON and I'm trying to parse it Java classes using GSON. Here is the JSON

resp = "{"isVisible":true,"image":{"preferenceOrder":["Rose","Lilly","Lotus"]}}";

my parse code for java is this.

ImageOrderResult result = new Gson().fromJson(resp,ImageOrderResult.class);

and here is the class which i have defined

public class ImageOrderResult {
    //Used for general Error Tracing
    public String status = "";
    public String message = "";
    public String errorTrace = "";

    public class Image{
        @SerializedName("preferenceOrder")
        public ArrayList<String> flowers= new ArrayList<String>();
    }

    @SerializedName("isVisible")
    public boolean isVisible= false; 
}    

Here i'm missing out the flowers array part. Parser is not able to fetch the list of values. How do I solve it?

Upvotes: 0

Views: 332

Answers (1)

Leon Lucardie
Leon Lucardie

Reputation: 9730

The problem is that you have the type of Image defined, but your class is missing a reference variable to actually "store" it in. You need to define your class like this for it to be properly serialized:

public class ImageOrderResult {
    //Used for general Error Tracing
    public String status = "";
    public String message = "";
    public String errorTrace = "";

    @SerializedName("image")
    public Image image = null;

    @SerializedName("isVisible")
    public boolean isVisible= false; 


    public class Image{
        @SerializedName("preferenceOrder")
        public ArrayList<String> flowers= new ArrayList<String>();
    }
}    

Upvotes: 2

Related Questions