使用Gson进行Json转换 [英] Json conversion using Gson
问题描述
您好我有以下类,需要使用Gson转换传入的Json
public class JsonConverter< T> {
private boolean success = Boolean.TRUE;
私人列表< T>数据;
private int total;
私人字符串消息;
public JsonConverter(){
}
public JsonConverter(List< T> data,int count){
this.data = data;
this.total = count;
} // getters和setters
我有一个传入的Json,其data属性包含一些具有以下定义的类的值:
class A {private long calendarId;
私有字符串标题;
私有字符串描述;
private int colorId;
private boolean isHidden;
私人长userId; // getter and setter
}
例如: 我想从 Json字符串中获取值,并将其设置在列表数据(应该是 A类对象列表)中使用Gson的JsonConverter类。 感谢 使用 Hi I have the following class and need to convert the incoming Json using Gson I have an incoming Json with a data property that contains the values of some a class which has the following definition: } Example:
Json string: {"data":{"calendarId":"ext-gen223","title":"work","description":"work","isHidden":false,"colorId":"2"}} I want to get the value from Json String and set it inside the List data (which should be a list of Class A objects) of Class JsonConverter using Gson. Thanks Used 这篇关于使用Gson进行Json转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
Json字符串:
lockquote
{calendarId:ext-gen223,title :work,description:work,isHidden:false,colorId:2}}
= new TypeToken< JsonConverter< A>>(){}。getType();
并且运行!public class JsonConverter<T>{
private boolean success = Boolean.TRUE;
private List<T> data;
private int total;
private String message;
public JsonConverter(){
}
public JsonConverter(List<T> data, int count){
this.data = data;
this.total = count;
} //getters and setters
class A {private long calendarId;
private String title;
private String description;
private int colorId;
private boolean isHidden;
private long userId; //getter and setter
Type type = new TypeToken<JsonConverter<A>>(){}.getType();
and got it running!