使用Gson进行Json转换 [英] Json conversion using Gson

查看:111
本文介绍了使用Gson进行Json转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有以下类,需要使用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字符串:

lockquote
{calendarId:ext-gen223,title :work,description:work,isHidden:false,colorId:2}}

我想从 Json字符串中获取值,并将其设置在列表数据(应该是 A类对象列表)中使用Gson的JsonConverter类。

感谢

解决方案

使用 = new TypeToken< JsonConverter< A>>(){}。getType(); 并且运行!


Hi I have the following class and need to convert the incoming Json using Gson

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

I have an incoming Json with a data property that contains the values of some a class which has the following definition:

class A {private long calendarId;
private String title;
private String description;
private int colorId;
private boolean isHidden;
private long userId; //getter and setter

}

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 Type type = new TypeToken<JsonConverter<A>>(){}.getType(); and got it running!

这篇关于使用Gson进行Json转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆