com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:期望一个字符串但是是BEGIN_OBJECT

时间:2014-08-06 14:58:04

标签: android json exception gson

我试图解析这个json

{"status":"ok","results":{"query":{"id":"6081","name":"UB1656-Palazzo Versace Resort in Culture Village","query":"Aswathy VS : <\/strong>
Test Query","add_date":"2014-08-05 08:27:45"},"replies":[{"replied_by":"Ventures Onsite","replied_on":"05-Aug-2014 09:05:19 AM","reply":"We have received your Test Query."},{"replied_by":"Me","replied_on":"05-Aug-2014 01:28:00 PM","reply":"Thanks... "}]}}

我的Pojo课程

import java.util.ArrayList;

public class QueryRepliesList {
    public String status;

public ArrayList<Result> results;

    public class Replies {

        public String id;
        public String name;
        public String query;
        public String add_date;
        public String replied_by;
        public String replied_on;
        public String reply;

    }

    public class Result {

        public ArrayList<Replies> query;
        public ArrayList<Replies> replies;


    }
}

我试着通过

来调用它
QueryRepliesList        queryReply = gson.fromJson(reader,
                            QueryRepliesList.class);

但是我得到了以下异常

  

引起:com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期字符串但是为BEGIN_OBJECT

我试过改变

根据问题的类似答案所建议的几次Pojo课程 但没有任何效果 请一些帮助 提前致谢

1 个答案:

答案 0 :(得分:5)

根据您提供的JSON,我没有看到您将如何获得提供的错误,而是您会看到

  

预计BEGIN_ARRAY但是BEGIN_OBJECT

因为resultsquery都是JSON_OBJECT。为了说明这一点,我已将所有内容都放入了junit测试中。

@org.junit.Test
public void asPosted() {
    final String json = "{'status':'ok','results':{'query':{'id':'6081','name':'UB1656-Palazzo Versace Resort in Culture Village','query':'Aswathy VS : <\\/strong>\n"
            + "Test Query','add_date':'2014-08-05 08:27:45'},'replies':[{'replied_by':'Ventures Onsite','replied_on':'05-Aug-2014 09:05:19 AM','reply':'We have received your Test Query.'},{'replied_by':'Me','replied_on':'05-Aug-2014 01:28:00 PM','reply':'Thanks... '}]}}";

    QueryRepliesContainer data = new Gson().fromJson(json, QueryRepliesContainer.class);
    Assert.assertEquals("ok", data.getStatus());
    Assert.assertEquals(2, data.getRepliesCount());
    Assert.assertEquals("6081", data.getQueryId());
}

通常,您会为每个JSON_OBJECT创建 POJO 。例外情况包括提供自定义反序列化器。取决于您需要展示&#34;内部&#34;的程度。数据,您需要将内部类迁移出来以进行公共级别的曝光。我将它们分别嵌套到JSON结构中。因此,根据提供的内容,您需要类似以下内容:

 class QueryRepliesContainer {

    private String status;
    private Result results;

    public String getStatus() {
        return status;
    }

    public int getRepliesCount() {

        return results.replies.size();
    }

    public String getQueryId() {
        return results.query.id;
    }

    private class Result {

        private Query query;
        private List<Replies> replies;

        private class Query {

            private String id;
            private String name;
            private String query;
        }

        private class Replies {

            private String add_date;
            private String replied_by;
            private String replied_on;
            private String reply;
        }
    }
}