0

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?

1 Answer 1

2

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>();
    }
}    
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I couldn't figure this at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.