我试图使用GSON将一些Json反序列化为一个漂亮,整洁的对象。现在,我已经设法让Json正确映射到一些更明显的变量。然而,在尝试绘制一些Json时,我遇到了这个:
{
"this_number": 1,
"that_number": 12,
"some_string": "String!",
"list_of_objects": {
"342356676784653234535345": {
"random_double": "0.1235667456456",
"magic": "29",
"health": 1,
"price": 7,
"point": {
"x": 2,
"y": 70
}
},
"2345263767467354": {
"random_double": "0.1235667456456",
"magic": "23",
"health": 1,
"price": 9,
"point": {
"x": 0,
"y": 70
}
}
}
}
直到我来到"list_of_objects"
之前,它的映射很好。我不能为我的生活找出如何实现它。我认为主要问题是它们不再是静态类名,它们是随机的。因此,写下类似的东西是完全不切实际的(也是不可能的):
class 342356676784653234535345{
double random_double = 0.0;
//etc
}
我已经浏览了Stackoverflow,但答案似乎相当复杂,很多人都不能完全回答我想知道的事情。
我使用了here的普通对象方法,但我无法找到有关其用法的更多信息。
我也一直在寻找映射到泛型类型的参考,但我不太了解发生了什么。 For example
答案 0 :(得分:4)
您可以使用自定义Gson JsonDeserializer
假设您有映射类
public class Data {
private int this_number;
private int that_number;
private String some_string;
private List<DataInfo> objects;
}
public class DataInfo {
private double random_double;
private int magic;
private int health;
private int price;
}
public class Point {
int x ;
int y;
}
CustomDeserializer
public class CustomDeserializer implements JsonDeserializer<Data> {
@Override
public Data deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException {
final JsonObject jsonObject = json.getAsJsonObject();
final int this_number = jsonObject.get("this_number").getAsInt();
final int that_number = jsonObject.get("that_number").getAsInt();
final String some_string = jsonObject.get("some_string").getAsString();
JsonObject list_of_objects =jsonObject.get("list_of_objects").getAsJsonObject();
Set<Entry<String, JsonElement>> objects = list_of_objects.entrySet();
final Data data = new Data();
List<DataInfo> list = new ArrayList<>();
Gson gson = new Gson();
for (Entry<String, JsonElement> entry : objects) {
JsonElement jsonElement = entry.getValue();
DataInfo info = gson.fromJson(jsonElement,DataInfo.class);
list.add(info);
}
data.setObjects(list);
data.setSome_string(some_string);
data.setThat_number(that_number);
data.setThis_number(this_number);
return data;
}
}
答案 1 :(得分:2)
只需定义
Map<String, Inner> list_of_objects;
在你的外课,让Gson为你工作。它会毫不费力地反序列化。为了使事情更清楚,我根据您的数据做了一个完整的例子。只需复制/粘贴/构建/运行此类。为方便起见,您的数据结构被定义为静态内部类,您可以将它们放在单独的文件中。
package stackoverflow.questions.q23472175;
import java.util.Map;
import com.google.gson.Gson;
public class Q23472175 {
private static class Point {
int x;
int y;
@Override
public String toString() {
return "Point [x=" + x + ", y=" + y + "]";
}
}
private static class Inner {
String random_double;
String magic;
int health;
int price;
Point point;
@Override
public String toString() {
return "Inner [random_double=" + random_double + ", magic=" + magic + ", health=" + health + ", price=" + price + ", point=" + point + "]";
}
}
private static class Outer {
int this_number;
int that_number;
String some_string;
Map<String, Inner> list_of_objects;
@Override
public String toString() {
return "Outer [this_number=" + this_number + ", that_number=" + that_number + ", some_string=" + some_string + ", list_of_objects=" + list_of_objects + "]";
}
}
public static void main(String[] args) {
String json = "{"+
" \"this_number\": 1,"+
" \"that_number\": 12,"+
" \"some_string\": \"String!\","+
" \"list_of_objects\": {"+
" \"342356676784653234535345\": {"+
" \"random_double\": \"0.1235667456456\","+
" \"magic\": \"29\","+
" \"health\": 1,"+
" \"price\": 7,"+
" \"point\": {"+
" \"x\": 2,"+
" \"y\": 70"+
" }"+
" },"+
" \"2345263767467354\": {"+
" \"random_double\": \"0.1235667456456\","+
" \"magic\": \"23\","+
" \"health\": 1,"+
" \"price\": 9,"+
" \"point\": {"+
" \"x\": 0,"+
" \"y\": 70"+
" }"+
" }"+
" }"+
"}";
Gson g = new Gson();
Outer object = g.fromJson(json, Outer.class);
System.out.print(object);
}
}
结果如下:
Outer [this_number=1, that_number=12, some_string=String!, list_of_objects={342356676784653234535345=Inner [random_double=0.1235667456456, magic=29, health=1, price=7, point=Point [x=2, y=70]], 2345263767467354=Inner [random_double=0.1235667456456, magic=23, health=1, price=9, point=Point [x=0, y=70]]}]