簡體   English   中英

GSON:使用隨機類名反序列化Json

[英]GSON: Deserializing Json with random class names

我正在嘗試使用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,但是答案似乎很復雜,許多答案並沒有完全回答我想知道的內容。

我已經在這里使用了普通的Object方法,但是我找不到有關其用法的更多信息。

我也一直在尋找對映射到泛型類型的引用,但我不太了解發生了什么。 例如

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

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

只需定義

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM