如何用Gson填充现有的递归对象

时间:2016-10-10 08:11:24

标签: java json gson

我想在现有对象中反序列化一个json字符串。我写了一个实例创建者,效果很好。但是如果object有一个具有相同结构的子对象(parent),我会得到一个意想不到的结果,因为Gson反序列化了子进程内的父对象。下面的代码打印:

Bart 10 Person@6aa5c9f1
Homer 36 Person@6aa5c9f1
Homer 36 Person@6aa5c9f1

但我期待:

Bart 10 Person@6aa5c9f1
Bart 20 Person@6aa5c9f1
Homer 36 Person@6aa5c9f1

由于

public class Test {

    public static void main(String[] args) {

        Person son = new Person();
        son.name = "Bart";
        son.age = 10;

        System.out.println(son.name + " " + son.age + " " + son);

        GsonBuilder builder = new GsonBuilder();
        builder.registerTypeAdapter(Person.class, new CustomInstanceCreator(son));
        Gson gson = builder.create();

        String json = "{\"age\":20,\"parent\":{\"name\":\"Homer\",\"age\":36}}";

        son = gson.fromJson(json, Person.class);

        System.out.println(son.name + " " + son.age + " " + son);

        System.out.println(son.parent.name + " " + son.parent.age + " " + son.parent);
    }
}


class Person {

    public String name;
    public int age;
    public Person parent;
}


class CustomInstanceCreator implements InstanceCreator<Person> {

    private Person person;

    public CustomInstanceCreator (Person person) {

        this.person = person;
    }

    public Person createInstance (Type type) {

        return person;
    }
}

2 个答案:

答案 0 :(得分:1)

我找到了解决方法

class CustomInstanceCreator implements InstanceCreator<Person> {

    private Person person;
    private boolean newInstance;

    public CustomInstanceCreator(Person person) {

        this.person = person;
        this.newInstance = false;
    }

    public Person createInstance(Type type) {

        Person instance = person;

        if (newInstance)
            instance = new Person();

        newInstance = true;

        return instance;
    }
}

答案 1 :(得分:0)

我没有使用Gson,而只使用了json。使用的库是json-simple-1.1.jar

public class JsonApiClass {

    public static void main(String[] args) {
        // TODO Auto-generated method stub


        try {

    String Response = "{\"Object1\":{\"Name1\":\"Bart\",\"age1\":\"10\"}"
                +"\n"+ "\"Object2\":{\"Name2\":\"Bart\",\"age2\":\"20\"}"
                +"\n"+ "\"Object3\":{\"Name3\":\"Homer\",\"age3\":\"36\"    }}";




            JSONParser parser = new JSONParser();

            try {

                Object obj = parser.parse(Response);

                JSONObject jsonObject = (JSONObject) obj;

                JSONObject jsonObject1  = (JSONObject)jsonObject.get("Object1");
                JSONObject jsonObject2  = (JSONObject)jsonObject.get("Object2");
                JSONObject jsonObject3  = (JSONObject)jsonObject.get("Object3");


                String name1 = (String)jsonObject1.get("Name1");
                String name2 = (String)jsonObject2.get("Name2");
                String name3 = (String)jsonObject3.get("Name3");

                Long age1 = new Long((String)jsonObject1.get("age1"));
                Long age2 = new Long((String)jsonObject2.get("age2"));
                Long age3 = new Long((String)jsonObject3.get("age3"));


            System.out.println(name1+" "+age1);
            System.out.println(name2+" "+age2);
            System.out.println(name3+" "+age3);




            } catch (Exception e) {
                e.printStackTrace();
            }



        } catch (Exception e) {
            System.out.println(e);
            e.printStackTrace();
        }

    }

输出

Bart 10
Bart 20
Homer 36

希望它能解决你的问题