2013-03-08 151 views
1

這是我的JSON字符串:JSON解析與GSON不工作

[{"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 

這是設置類,實體是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之後的列表爲空。這段代碼有什麼問題?

請幫

回答

1

您應該使用getType()而不是getClass()

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