复杂的json到gson

时间:2016-01-12 08:08:16

标签: android json gson

我有一个类似于下面的json数据:

{
    total_result: 46979,
    facet: [
        {
        author: [
            {
                writter1: 409
            },
            {
                writter2: 390
            },
            {
                writter3: 273
            },
            ........................
            ......................
        ]
        },
        {
        category: [
            {
                book1: 40480
            },
            {
                book2: 6178
            },
            {
                book3: 5994
            },
            .....................
            .....................
        ]
        },
    .......................
    ......................
    ],
    results:[.........]
}

facet下的数据是动态的。 我怎样才能将这个json格式化为gson?

我的代码:

public class ResultContainer {
    public int total_result;
    public Facet facet; 
    public List<Result> results;
}

和Facet类是:

public class Facet {
    private List<Map<String, String>> author;
    private List<Map<String, String>> category;
    private List<Map<String, String>> domain;
    private List<Map<String, String>> editor;
    private List<Map<String, String>> publisher;
    private List<Map<String, String>> tags;

    // rest of the code is getter and setter
}

问题在于facet部分。 json的所有其他部分都正确地解析为gson。但不是facet。如果我阻止public Facet facet; ResultContainer,则代码可以正常运行。有什么问题?

但它不起作用。我该如何解决这个问题?

错误:

01-12 14:32:05.248: E/AndroidRuntime(19591): FATAL EXCEPTION: main
01-12 14:32:05.248: E/AndroidRuntime(19591): Process: com.pipilika.booksearch, PID: 19591
01-12 14:32:05.248: E/AndroidRuntime(19591): com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 32 path $.facet
01-12 14:32:05.248: E/AndroidRuntime(19591):    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:221)
01-12 14:32:05.248: E/AndroidRuntime(19591):    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:117)
01-12 14:32:05.248: E/AndroidRuntime(19591):    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:217)
01-12 14:32:05.248: E/AndroidRuntime(19591):    at com.google.gson.Gson.fromJson(Gson.java:861)
01-12 14:32:05.248: E/AndroidRuntime(19591):    at com.google.gson.Gson.fromJson(Gson.java:826)
01-12 14:32:05.248: E/AndroidRuntime(19591):    at com.google.gson.Gson.fromJson(Gson.java:775)
01-12 14:32:05.248: E/AndroidRuntime(19591):    at com.google.gson.Gson.fromJson(Gson.java:747)
01-12 14:32:05.248: E/AndroidRuntime(19591):    at com.pipilika.booksearch.MainActivity$AsyncBookSearch.onPostExecute(MainActivity.java:235)
01-12 14:32:05.248: E/AndroidRuntime(19591):    at com.pipilika.booksearch.MainActivity$AsyncBookSearch.onPostExecute(MainActivity.java:1)
01-12 14:32:05.248: E/AndroidRuntime(19591):    at android.os.AsyncTask.finish(AsyncTask.java:632)
01-12 14:32:05.248: E/AndroidRuntime(19591):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
01-12 14:32:05.248: E/AndroidRuntime(19591):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
01-12 14:32:05.248: E/AndroidRuntime(19591):    at android.os.Handler.dispatchMessage(Handler.java:106)
01-12 14:32:05.248: E/AndroidRuntime(19591):    at android.os.Looper.loop(Looper.java:189)
01-12 14:32:05.248: E/AndroidRuntime(19591):    at android.app.ActivityThread.main(ActivityThread.java:5528)
01-12 14:32:05.248: E/AndroidRuntime(19591):    at java.lang.reflect.Method.invoke(Native Method)
01-12 14:32:05.248: E/AndroidRuntime(19591):    at java.lang.reflect.Method.invoke(Method.java:372)
01-12 14:32:05.248: E/AndroidRuntime(19591):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:950)
01-12 14:32:05.248: E/AndroidRuntime(19591):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
01-12 14:32:05.248: E/AndroidRuntime(19591): Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 32 path $.facet
01-12 14:32:05.248: E/AndroidRuntime(19591):    at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:388)
01-12 14:32:05.248: E/AndroidRuntime(19591):    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:210)
01-12 14:32:05.248: E/AndroidRuntime(19591):    ... 18 more

1 个答案:

答案 0 :(得分:0)

错误是不言自明的。 json结构包含一个&#39; facet&#39;但是你的类结构在哪里为facet声明了一个对象。更改您的类结构,以便有一个方面列表,如下所示:

public class ResultContainer {
    public int total_result;
    public List<Facet> facet; // this is the required change
    public List<Result> results;
}