【问题标题】:GSON: Deserializing Json with random class namesGSON:使用随机类名反序列化 Json
【发布时间】:2014-06-21 17:41:40
【问题描述】:

我正在尝试使用 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 的普通 Object 方法,但我找不到任何有关其用法的更多信息。

我也一直在寻找映射到泛型类型的引用,但我不太明白发生了什么。 For example

【问题讨论】:

  • 如果这些只是类实例的名称,请使用 Map<String, YourType> 并将 YourType 实现为具有字段 random_double 等。

标签: java gson


【解决方案1】:

您可以使用自定义 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;
}

自定义解串器

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的吗?
  • 这里也没有使用Point
【解决方案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]]}]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多