Gson:预计begin_array但是STRING

时间:2014-04-01 19:53:22

标签: java android json gson

我在解析JSON数据时面临以下错误:

预计begin_array,但STRING在第1行第34列

我找不到解决方案。我的JSON如下:

   {"result":0,"count":2,"records":"[{\"name\":\"name1\",
    \"id\":\"28\",
 \"photo\":\"\\\/gallery\\\/c9\\\/f0f8\\\/95fb\\\/c9f0f895fb98ab9159f51fd0297e236d\\\/28\\\/tb_uykzubjqmxbv6zkogdsd_c64962310e572f5e8a4c73a44a4fa3dd.jpg\"},{\"name\":\"name2\",
\"id\":\"134\",
\"photo\":\"\\\/gallery\\\/c9\\\/f0f8\\\/95fb\\\/c9f0f895fb98ab9159f51fd0297e236d\\\/134\\\/tb_23ffxuw9-5ys4iqtwztz_610982fa52cca03412dbc84ab0ea5e18.jpg\"}]"}

这是我的PersonContent类:

public class PersonContent {

    @SerializedName("result")
    public int result;
    @SerializedName("count")
    public int  count;
    @SerializedName("records")
    List<Person> records;


    public List<Person> getRecords() {
        return records;
    }

    public void setRecords(List<Person> records) {
        this.records = records;
    }

    public void setResults(int result) {
        this.result = result;
    }

    public int getResults(){
        return result;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public int getCount(){
        return count;
    }

以下是Person Class:

public class Person implements Serializable {
        private static final long serialVersionUID = 1L;

        @SerializedName("name")
        public String name;
        @SerializedName("id")
        public String id;
        @SerializedName("photo")
        public String photo;


        public Person(){

        }
        public Person( String name,String id,String photo) {
            this.id = id;
            this.name = name;
            this.photo = photo;

        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getPhoto() {
            return photo;
        }

        public void setPhoto(String photo) {
            this.photo = photo;
        }

    @Override
    public String toString() {
        return name;
    }
}

以下是我反序列化前面提到的JSON数据的代码

private void Parse(){
InputStream source = retrieveStream(url);
Gson gson = new Gson();
Reader reader = new InputStreamReader(source);  
personlist = gson.fromJson(reader, PersonContent.class);
}

我已经尝试了在这里找到的所有解决方案,但我找不到相同的JSON。 还有以下内容:com.google.gson.JsonSyntaxException: Expected BEGIN_ARRAY but was STRING

1 个答案:

答案 0 :(得分:8)

错误发生在您收到的json中: 你的类需要一个数组,因为

 List<Person> records;

然后在你的json中你有

&#34;记录&#34;:的&#34; [

必须是:

"records": [

并在最后]&#34;},必须是

]}