2
I am trying to parse a JSON, using GSON, but there is a complex part, because I don’t know the name of Keys, so I don’t know what name I should use in the variables for GSON to parse. I researched and it seems that I have to use Map<>, but I’m not even able to do it.
Example JSON:
{
"type": "typeString",
"format": "json",
"version": "4.13.35",
"data": {
"data1": {
"version": "4.13.1",
"id": "data1",
"key": "238",
"name": "John"
},
"data2": {
"version": "4.13.1",
"id": "data2",
"key": "115",
"name": "Hello"
},
"data3": {
"version": "4.13.1",
"id": "date3",
"key": "26",
"name": "Zeus"
},
"data4": {
"version": "4.13.1",
"id": "data4",
"key": "143",
"name": "Venus"
}
}
}
Made class:
public class MasterGSON {
@SuppressWarnings("type")
private String type;
@SuppressWarnings("format")
private String format;
@SuppressWarnings("version")
private String version;
@SuppressWarnings("data")
private Map<String, DataGSON> listChampion;
public Map<String, DataGSON> getListChampion() {
return listChampion;
}
public String getType() {
return type;
}
public String getFormat() {
return format;
}
public String getVersion() {
return version;
}
public class DataGSON {
private String version;
private String id;
private String key;
private String name;
public String getVersion() {
return version;
}
public String getId() {
return id;
}
public String getKey() {
return key;
}
public String getName() {
return name;
}
}
}
Can someone help me?
Kiotto, include the code you have already done, as the question is very wide. How come you do not know the keys? Would the attributes of JSON not? Can these attributes that are in the example JSON change? That I know GSON can fill its class using
Reflection
in the fields.– Wakim
The code I made was the one on top, then I do: Gson gsonaux = new Gsonbuilder(). create(); Mastergson aux = gsonaux.fromJson(json, Mastergson.class); The variables type, format, version come with data, listChampion comes null... I don’t know the Keys data1, data2, data3... These Keys can change and can have N Keys
– Kiotto
Hey, Kiotto, when people have questions about your question, you better [Edit] the question to clarify things. It is confusing if you put this kind of thing in the comments.
– brasofilo