0

我正在尝试将从服务器获取的 JSON 反序列化为其原始 POJO 形式。我知道原始对象(我有服务器代码的来源),但似乎我遗漏了一些东西。

这是我可以组装的最小代码示例,它说明了我的问题:

package mycompany.mypackage;
import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.InstanceCreator;

public class GsonSerializeTest {

    /* main method to illustrate the problem */
    public static void main(String[] args) {
        Serializable[] identifiers= {"ITEM",12345678,"abc.def.ghijkl.mnopqr",87654321};
        EntityUid uid = new GsonSerializeTest().new EntityUid(identifiers);
        Gson converter = new GsonBuilder().registerTypeAdapter(Serializable.class, new GsonSerializeTest().new SerializableInstanceCreator()).create();
        String json = converter.toJson(uid);
        System.out.println("Converted to string: " + json);
        EntityUid uid2 = converter.fromJson(json, EntityUid.class); // ERROR
        System.out.println("Converted back to object: " + uid2);
    }

    /* the POJO that gets serialized and fails while deserializing */
    public class EntityUid implements Serializable {
        private final List<Serializable> identifier = new ArrayList<Serializable>();

        public EntityUid(final Serializable... identifier) {
            for (Serializable partialIdentifier : identifier) {
                this.identifier.add(partialIdentifier);
            }
        }
    }

    /* Class for generating Serializable instances */
    public class SerializableInstanceCreator implements InstanceCreator<Serializable> {
        public Serializable createInstance(Type arg0) {
            return new String();
        }
    }
}

这准备了 EntityUid 对象,将它(就像服务器一样)序列化为:

Converted to string: {"identifier":["ITEM",12345678,"abc.def.ghijkl.mnopqr",87654321]}

然后尝试将其反序列化为原始对象,但失败并显示以下内容:

Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 17
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:81)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)
    at com.google.gson.Gson.fromJson(Gson.java:803)
    at com.google.gson.Gson.fromJson(Gson.java:768)
    at com.google.gson.Gson.fromJson(Gson.java:717)
    at com.google.gson.Gson.fromJson(Gson.java:689)
    at mycompany.mypackage.GsonSerializeTest.main(GsonSerializeTest.java:19)
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 17
    at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:374)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:165)
    ... 10 more

有人可以为我提供一个关于我可能缺少什么的指针吗?提前非常感谢!

4

2 回答 2

2

请阅读使用任意类型的对象对集合进行序列化和反序列化

下面的代码将起作用。

import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class GsonSerializeTest {

    /* main method to illustrate the problem */
    public static void main(String[] args) {
        Object[] identifiers= {12345678,"ITEM","abc.def.ghijkl.mnopqr",87654321};
        EntityUid uid = new EntityUid(identifiers);
        Gson converter = new GsonBuilder().create();
        String json = converter.toJson(uid);
        System.out.println("Converted to string: " + json);
        EntityUid uid2 = converter.fromJson(json, EntityUid.class); // ERROR
        System.out.println("Converted back to object: " + uid2);
    }

    /* the POJO that gets serialized and fails while deserializing */
    public static class EntityUid  {
        private final List<Object> identifier = new ArrayList<Object>();

        public EntityUid(final Object... identifier) {
            for (Object partialIdentifier : identifier) {
                this.identifier.add(partialIdentifier);
            }
        }

        @Override
        public String toString() {
            return "EntityUid [identifier=" + identifier + "]";
        }


    }
}
于 2013-08-27T10:40:35.660 回答
0

浏览下面的帖子,它会对你有所帮助。

Android JSON 错误“预期为 BEGIN_OBJECT 但在第 1 行第 2 列为 BEGIN_ARRAY”

收藏限制:

可以序列化任意对象的集合但不能从中反序列化 因为用户无法指示结果对象的类型 在反序列化时,Collection 必须是特定的泛型类型 所有这些都是有道理的,而且很少有问题遵循良好的 Java 编码实践时

于 2013-08-27T10:37:30.143 回答