Json使用gson解析不起作用

时间:2013-03-08 15:46:17

标签: android json gson

这是我的JSON字符串:

[{"BranchID":1,"SecurityCode1":13,"SecurityCode2":14,"PrintHeight":10,"PrintWidth":10,"Active":true}]

这是我用来解析JSON的代码:

Type t = new TypeToken<List<Setting>>() {
}.getClass();

String json =  ServiceHelper.getJSON(response); 
List<Setting> list = (List<Setting>) gson.fromJson(json, t); 
    //list is null which it souldnt

这是Setting类,Entity是ORMDroid实体类:

public class Setting extends Entity {

@Column(name = "id", primaryKey = true)
@SerializedName("BranchID")
public int BranchID;

public int securityCodeLPK;
public int securityCodeSDK;

@SerializedName("PrintHeight")
public int PrintHeight;

@SerializedName("PrintWidth")
public int PrintWidth;

@SerializedName("Active")
public String Active;

@SerializedName("SecurityCode1")
public String SecurityCode1;

@SerializedName("SecurityCode2")
public String SecurityCode2;

public Setting(int BranchID, int height, int width, String active, String securityCode1, String securityCode2) {
    super();
    BranchID = BranchID;
    PrintHeight = height;
    PrintWidth = width;
    Active = active;
    SecurityCode1 = securityCode1;
    SecurityCode2 = securityCode2;
}

public Setting () {
    super();
}

}

似乎没问题,但是gson.FromJson之后的列表为null。这段代码出了什么问题?

请帮忙

1 个答案:

答案 0 :(得分:1)

您应该使用getType()代替getClass()

Type t = new TypeToken<List<Setting>>() {}.getType();