繁体   English   中英

使用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而从Check类获取任何内容。

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”不是字符串数组。 这是一个对象。

因此,您需要一个类似的类:

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

并修改您的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 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

例如:在声明字符串客户端之前添加@SerializedName(“ client”)

@SerializedName("client")
public String client;

您可以一次完成这些操作,而不必重复编写代码。 检查一下-http: //www.jsonschema2pojo.org/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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