gson.fromJson为动态键返回null

时间:2015-03-10 09:50:51

标签: android json gson

我试图在我的Android应用程序中使用gson解析以下响应:

{
    "richerich":{
        "id":19250176,
        "name":"RichERich",
        "profileIconId":744,
        "summonerLevel":30,
        "revisionDate":1425977347000
    }

    "alma":{
        "id":19250174,
        "name":"Alma",
        "profileIconId":764,
        "summonerLevel":30,
        "revisionDate":14259773423424
    }
}

关键" richeric"是一个动态的键,可能会改变,我也可以有其他响应对象,如" richeric"在我的回复字符串中。

我为此创建了类:

public class SummonerDto {
    private long id;
    private String name;
    private int profileIconId;
    private long revisionDate;
    private long summonerLevel;

    //getters, setters...
}

和我的回复课:

public class SummonerInfoResponse {

    private Map<String, SummonerDto> summoners;

    public Map<String, SummonerDto> getSummoners() {
        return summoners;
    }

    public void setSummoners(Map<String, SummonerDto> summoners) {
        this.summoners = summoners;
    }
}

我使用以下代码:

return gson.fromJson(response, SummonerInfoResponse.class);

但它返回null。谁能告诉我为什么?

感谢。

3 个答案:

答案 0 :(得分:1)

如果没有SummonerDto

,您应该能够获得SummonerInfoResponse

这是我的主要内容:

String res = "{\"richerich\":{\"id\":19250176,\"name\":\"RichERich\",\"profileIconId\":744,\"summonerLevel\":30,\"revisionDate\":1425977347000}}";
Gson gson = new Gson();         
Map<String, SummonerDto> decoded = gson.fromJson(res, new TypeToken<Map<String, SummonerDto>>(){}.getType());

System.out.println(decoded.get("richerich").getName());

You can get more info from this similar issue.

答案 1 :(得分:1)

我猜你试图在某些方面获得arraylist。我不能直接回答你在哪里犯错误,因为这对于Gson的工作方式是不明确的。我在下面给出了一个测试代码,您可以运行它并根据需要进行更改,您将理解为什么您无法在代码中进行转换。

public class GsonTestFour {

public static void main(String[] args) {

    ArrayList<UserInfo> arrayList = new ArrayList<>();
    HashMap<String, UserInfo> hashMap = new HashMap<String, UserInfo>();

    UserInfo info1 = new UserInfo(0, "a", 7000, 5, 10);
    UserInfo info2 = new UserInfo(0, "b", 7050, 5, 10);
    UserInfo info3 = new UserInfo(0, "c", 7900, 5, 10);
    UserInfo info4 = new UserInfo(0, "d", 7060, 5, 10);
    UserInfo info5 = new UserInfo(0, "e", 7007, 5, 10);

    arrayList.add(info1);
    arrayList.add(info2);
    arrayList.add(info3);
    arrayList.add(info4);
    arrayList.add(info5);

    System.out.println(""+(new Gson()).toJson(arrayList));

    for (int i = 0; i < arrayList.size(); i++) {
        hashMap.put(arrayList.get(i).getUsername(), arrayList.get(i));
    }
    System.out.println("\n");
    for (int i = 0; i < hashMap.size(); i++) {
        UserInfo info = hashMap.get(arrayList.get(i).getUsername());
        System.out.println("info.getUsername()=> "+info.getUsername());
        System.out.println("info.getBalance()=> "+info.getBalance());
    }

}

}

您将需要此数据类型

public class UserInfo {

private String username = "";
private double balance = 0;
private int selectedImage = -1;
private int bet = 0;
private int action = 0;

public UserInfo(int action, String username, double balance, int selectedImage, int bet) {
    // TODO Auto-generated constructor stub
    this.action = action;
    this.username = username;
    this.balance = balance;
    this.selectedImage = selectedImage;
    this.bet = bet;
}

public String getUsername() {
    return username;
}

public double getBalance() {
    return balance;
}

public int getSelectedImage() {
    return selectedImage;
}

public int getBet() {
    return bet;
}

public int getAction() {
    return action;
}

public void setBet(int bet) {
    this.bet = bet;
}
}

并且不要放弃Gson它是非常好的库。它节省了很多编码时间

答案 2 :(得分:0)

我找到了解决方案。

我使用HashMap扩展了我的响应类,现在我可以将响应作为gson对象获取。这是完整的代码:

public class SummonerInfoResponse extends HashMap<String, SummonerDto> {


}

并使用此:

return gson.fromJson(response, SummonerInfoResponse.class);