GSON:用随机类名称反序列化Json [英] GSON: Deserializing Json with random class names

查看:94
本文介绍了GSON:用随机类名称反序列化Json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用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,
魔术:23,
健康:1,
价格:9,
点数:{
x:0,
y:70
}
}
}
}

它很好地映射,直到我来到list_of_objects。我不能为我的生活制定出如何实施它。我认为主要问题是它们不再是静态类名,它们是随机的。因此,写下如下内容是完全不切实际的(也是不可能的):

  class 342356676784653234535345 {
double random_double = 0.0 ;
// etc
}

我查看了一下Stackoverflow,但答案似乎相当复杂,许多人并没有完全回答我想知道的内容。



我玩过简单的Object方法,使用这里,但我找不到任何关于它的用法的更多信息。



我也继续找到映射到泛型的引用,但我不太明白发生了什么。 例如

JsonDeserializer



假设您有映射类

  public class Data {
private int this_number;
private int that_number;
private String some_string;
私人清单< DataInfo>对象;
}

公共类DataInfo {
private double random_double;
private int magic;
private int health;
私有诠释价格;
}

public class Point {
int x;
int y;

CustomDeserializer

  public class CustomDeserializer实现了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();

设置< Entry< String,JsonElement>> objects = list_of_objects.entrySet();

final Data data = new Data();
清单< DataInfo> list = new ArrayList<>();

Gson gson = new Gson(); (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);

返回数据;
}
}


I'm trying to use GSON to deserialize some Json to a nice, tidy object. Now, I have managed to get the Json to correctly map to some of the more obvious variables. However, while trying to map some of the Json I came across this:

{
    "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
            }
        }
    }
}

It was mapping nicely until I came to "list_of_objects". I can't for the life of me work out how to implement it. I think the main issue is that they are no longer static class names, they are randomized. Therefore it would be totally impractical (and impossible) to write something like:

class 342356676784653234535345{
    double random_double = 0.0;
    //etc
}

I've had a look around Stackoverflow, but the answers seem quite complex and many don't quite answer what I'm wanting to know.

I have played around with the plain Object method used here, but I couldn't find any further information on its usage.

I also keep finding references to mapping to generic types, but I don't quite understand what is going on. For example

解决方案

You can convert a JSON string to an equivalent Java object using custom Gson JsonDeserializer

Assuming you have mapping classes

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;
    }
}

这篇关于GSON:用随机类名称反序列化Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆