使用 gson 将 Json 转换为 Android 中的对象

Convert Json to an object in Android with gson

我有一个包含很多条目的 JSON 数组,我想使用 gson 将每个检查反序列化为单个对象。我的问题是为它找到合适的对象结构。

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
        }
    }, ....
] 

我尝试了下面的对象,但它没有用,它只是得到 String client 但没有从检查 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;
}

当我们从 http://jsonviewer.stack.hu/ 检查您的 json 时,我们发现 "subdue" 不是字符串数组。这是一个对象。

所以你需要 class 比如:

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

并修改您的支票 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 Subdue subdue;
    public String name;
    public int issued;
    public int executed;
    public float duration;
    public String output;
    public int status;
}

我看不出任何其他问题。我希望这会对你有所帮助。祝你好运。

你需要像这样设置注释才能使用 GSON

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

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

制作GSON需要加注解

例如:在声明字符串 client

之前添加 @SerializedName("client")
@SerializedName("client")
public String client;

无需重复编写代码,您可以一次性完成这些。检查这个 - http://www.jsonschema2pojo.org/