问题
I am trying to parse the following response using gson in my android app:
{
"richerich":{
"id":19250176,
"name":"RichERich",
"profileIconId":744,
"summonerLevel":30,
"revisionDate":1425977347000
}
"alma":{
"id":19250174,
"name":"Alma",
"profileIconId":764,
"summonerLevel":30,
"revisionDate":14259773423424
}
}
The key "richeric" is a dynamic key and may change, and i can also have other response objects like "richeric" in my response string.
I create classes for this:
public class SummonerDto {
private long id;
private String name;
private int profileIconId;
private long revisionDate;
private long summonerLevel;
//getters, setters...
}
and my response class:
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;
}
}
and i use the following piece of code:
return gson.fromJson(response, SummonerInfoResponse.class);
but it returns null. Can anyone tell me why?
Thanks.
回答1:
You should be able to get SummonerDto without SummonerInfoResponse
Here is my main:
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.
回答2:
I guess you are trying to get arraylist in some ways. I can't give you a direct answer where are you making error, Because that would be ambiguous to how exactly Gson works. I have given a test code below you can run it and make changes as you fit you will understand why you are not able to convert it in your code.
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());
}
}
}
you will need this data type
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;
}
}
and don't give up on Gson it's really good library. It saves a lot of time in coding
回答3:
I found the solution.
I extended my response class with HashMap and now i can get the response as a gson object. Here is the full code:
public class SummonerInfoResponse extends HashMap<String, SummonerDto> {
}
and use this:
return gson.fromJson(response, SummonerInfoResponse.class);
来源:https://stackoverflow.com/questions/28960492/gson-fromjson-returns-null-for-dynamic-keys