[英]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. 我有一个包含大量条目的JSON数组,并希望使用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 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. 我尝试了下面的对象,但是它没有用,它只是获取
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;
}
When we check your json from http://jsonviewer.stack.hu/ we see that "subdue" is not a String Array. 当我们从http://jsonviewer.stack.hu/检查您的json时,我们看到“ subdue”不是字符串数组。 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: 并修改您的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;
}
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 您需要像这样设置注释才能使用GSON
public class Client {
@SerializedName("client")
public String client;
@SerializedName("check")
public Check check;
}
Annotation needs to be added to make GSON 需要添加注释以使GSON
eg: Add @SerializedName("client") before declaring the string client 例如:在声明字符串客户端之前添加@SerializedName(“ client”)
@SerializedName("client")
public String client;
Instead of writing code repeatedly, you can do these in one go. 您可以一次完成这些操作,而不必重复编写代码。 check this - http://www.jsonschema2pojo.org/
检查一下-http: //www.jsonschema2pojo.org/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.