Skip to content Skip to sidebar Skip to footer

Convert Json To An Object In Android With Gson

I have a JSON array with a lot of entries and want to deserialize each check into a single object with gson. My problem is to find the proper object structure for it. Gson gson =

Solution 1:

You need to set the annotation like this to use GSON

publicclassClient{
        @SerializedName("client")public String client;

        @SerializedName("check")public Check check;
    }

Solution 2:

When we check your json from http://jsonviewer.stack.hu/ we see that "subdue" is not a String Array. It's an object.

So you need a class like:

publicclassSubdue{
    publicString at;
    publicString begin;
    publicString end;
}

and modify your Check Class like:

publicclassCheck{
    publicBoolean handle;
    publicBoolean standaloone;
    publicint interval;
    publicint refresh;
    publicString[] dependencies;
    publicString commmand;
    publicint occurrences;
    publicString[] subscribers;
    publicBoolean aggregate;
    public Subdue subdue;
    publicString name;
    publicint issued;
    publicint executed;
    publicfloat duration;
    publicString output;
    publicint status;
}

I couldn't see any other problems. I hope this'll help you. Good Luck.

Solution 3:

Annotation needs to be added to make GSON

eg: Add @SerializedName("client") before declaring the string client

@SerializedName("client")public String client;

Instead of writing code repeatedly, you can do these in one go. check this - http://www.jsonschema2pojo.org/

Post a Comment for "Convert Json To An Object In Android With Gson"