这是我的 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后面的list是null。这段代码有什么问题?
请帮忙
最佳答案
您应该使用 getType()
而不是 getClass()
。
Type t = new TypeToken<List<Setting>>() {}.getType();
关于android - Json 解析与 gson 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15297839/