1

这是我的 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 之后的列表为空。这段代码有什么问题?

请帮忙

4

1 回答 1

1

您应该使用getType()而不是getClass().

Type t = new TypeToken<List<Setting>>() {}.getType();
于 2013-03-08T16:44:23.037 回答