简体   繁体   中英

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 = new Gson();
Type collectionType = new TypeToken<List<Client>>(){}.getType();
List<Client> clients = gson.fromJson(response, collectionType);

JSON

[
    {
        "client": "client1",
        "check": {
            "handle": true,
            "standalone": false,
            "interval": 60,
            "refresh": 3600,
            "dependencies": [
                "keepalive"
            ],
            "command": "xxx",
            "occurrences": 1,
            "subscribers": [
                "xxx"
            ],
            "aggregate": false,
            "subdue": {
                "at": "handler",
                "begin": "6PM ICT",
                "end": "9AM ICT"
            },
            "name": "xxx",
            "issued": 1437922960,
            "executed": 1437922960,
            "duration": 0.148,
            "output": "xxx",
            "status": 0
        }
    },
    {
        "client": "client1",
        "check": {
            "thresholds": {
                "warning": 120,
                "critical": 180
            },
            "handler": "keepalive",
            "name": "keepalive",
            "issued": 1437922959,
            "executed": 1437922959,
            "output": "Keepalive sent from client 13 seconds ago",
            "status": 0
        }
    }, ....
] 

I tried the object below but it didnt work, it just gets String client but nothing from the Check class.

public class Check {
    public Boolean handle;
    public Boolean standaloone;
    public int interval;
    public int refresh;
    public String[] dependencies;
    public String commmand;
    public int occurrences;
    public String[] subscribers;
    public Boolean aggregate;
    public String[] subdue;
    public String name;
    public int issued;
    public int executed;
    public float duration;
    public String output;
    public int status;
}

public class Client {
    public String client;
    public Check check;
}

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:

public class Subdue {
    public String at;
    public String begin;
    public String end;
}

and modify your Check Class like:

public class Check {
    public Boolean handle;
    public Boolean standaloone;
    public int interval;
    public int refresh;
    public String[] dependencies;
    public String commmand;
    public int occurrences;
    public String[] subscribers;
    public Boolean aggregate;
    public Subdue subdue;
    public String name;
    public int issued;
    public int executed;
    public float duration;
    public String output;
    public int status;
}

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

You need to set the annotation like this to use GSON

public class Client {
        @SerializedName("client")
        public String client;

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

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM