java - Gson:应为 begin_array 但为 STRING

标签 java android json gson

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

应为 begin_array 但在第 1 行第 34 列为 STRING

我找不到解决方案。我的 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类:

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

最佳答案

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

 List<Person> records;

但是在你的 json 中你有

“记录”:[

它必须是:

"records": [

最后 ]"},必须是

]}

关于java - Gson:应为 begin_array 但为 STRING,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22795614/

相关文章:

java - JTextArea 中的国际化字符串

android - 启动 Intent 时相机在 Android 10 上崩溃

java - ActiveMQ http 连接错误

java - 重用复杂的 spring-fox swagger 注释

java - openapi-code-generator 3.3.4 不读取标签并且 API 类名不是预期的

android - EditText 具有单文本行、换行和完成操作?

java - Android - 清除导航后退栈

javascript - 按名称排序数据而不是默认的 json/c#

javascript - 按对象值加载 JSON 文件

javascript - 如何使用 Ajax 将 WebSocket 数据从 View 发送到 Controller 以及将修改后的数据发送到 View