2013-03-08 119 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();