Java Gson:应为 begin_array 但为 STRING

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22795614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:56:35  来源:igfitidea点击:

Gson: Expected begin_array but was STRING

javaandroidjsongson

提问by klgr

I am facing the following error in parsing JSON data:

我在解析 JSON 数据时遇到以下错误:

Expected begin_array but was STRING at line 1 column 34

预期 begin_array 但在第 1 行第 34 列处为 STRING

I cannot find a solution. My JSON is the following:

我找不到解决方案。我的 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\"}]"}

This is my PersonContent Class:

这是我的 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;
    }

and the following is the Person Class:

以下是 Person 类:

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;
    }
}

And here is the code where i Deserialize the previous mentioned JSON Data

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

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

I have tried all solutions found in here, but I could not find an identical JSON. Also the following:com.google.gson.JsonSyntaxException: Expected BEGIN_ARRAY but was STRING

我已经尝试了此处找到的所有解决方案,但找不到相同的 JSON。还有以下内容:com.google.gson.JsonSyntaxException:预期为 BEGIN_ARRAY 但为 STRING

采纳答案by kalin

the error is in the json you receive: you class expects an array because

错误在您收到的 json 中:您的课程需要一个数组,因为

 List<Person> records;

but then in your json you have

但是在你的 json 中你有

"records":"[

“记录”:[

it has to be :

它一定要是 :

"records": [

and at the end ]"}, has to be

最后 ]"} 必须是

]}