Java GSON:使用随机类名反序列化Json
我试图使用GSON将一些Json反序列化为一个漂亮整洁的对象。现在,我已经设法让Json正确地映射到一些更明显的变量。然而,在尝试映射一些Json时,我遇到了以下问题:Java GSON:使用随机类名反序列化Json,java,gson,Java,Gson,我试图使用GSON将一些Json反序列化为一个漂亮整洁的对象。现在,我已经设法让Json正确地映射到一些更明显的变量。然而,在尝试映射一些Json时,我遇到了以下问题: { "this_number": 1, "that_number": 12, "some_string": "String!", "list_of_objects": { "342356676784653234535345": { "random_doubl
{
"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
}
}
}
}
它映射得很好,直到我来到“对象列表”
。我一辈子都想不出如何实施它。我认为主要的问题是它们不再是静态的类名,而是随机化的。因此,写这样的东西是完全不切实际的(也是不可能的):
class 342356676784653234535345{
double random_double = 0.0;
//etc
}
我已经环顾了Stackoverflow,但答案似乎相当复杂,许多答案并不完全符合我想知道的
我已经研究过使用的普通对象方法,但是我找不到关于其用法的任何进一步信息
我也一直在寻找映射到泛型类型的引用,但我不太明白到底发生了什么 您可以使用自定义Gson
JsonDeserializer将JSON字符串转换为等效的Java对象
假设您有映射类
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;
}
公共类数据{
私人int此_编号;
私人int表示_编号;
私有字符串一些_字符串;
私有列表对象;
}
公共类数据信息{
私人双随机双;
私人魔法;
私人卫生;
私人int价格;
}
公共课点{
int x;
int-y;
}
自定义反序列化程序
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;
}
}
公共类CustomDeserializer实现JsonDeserializer{
@凌驾
公共数据反序列化(最终JsonElement json、最终类型typeOfT、最终JsonDeserializationContext上下文)引发JsonParseException{
final JsonObject JsonObject=json.getAsJsonObject();
final int this_number=jsonObject.get(“this_number”).getAsInt();
final int that_number=jsonObject.get(“that_number”).getAsInt();
最后一个字符串some_String=jsonObject.get(“some_String”).getAsString();
JsonObject list_of_objects=JsonObject.get(“list_of_objects”).getAsJsonObject();
Set objects=list_of_objects.entrySet();
最终数据=新数据();
列表=新的ArrayList();
Gson Gson=新的Gson();
for(条目:对象){
JsonElement JsonElement=entry.getValue();
DataInfo=gson.fromJson(jsonElement,DataInfo.class);
列表。添加(信息);
}
数据集对象(列表);
data.setSome_字符串(some_字符串);
数据。设置该号码(该号码);
数据。设置此_编号(此_编号);
返回数据;
}
}
只需定义
Map<String, Inner> list_of_objects;
如果这些只是类实例的名称,请使用
Map
并将YourType
实现为具有字段randoom\u double
,等等。您可以添加如何调用CustomDeserializer吗?这也不使用点
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]]}]