当前位置: 首页 > 面试题库 >

Gson:应为begin_array,但为STRING

孟振
2023-03-14
问题内容

我在解析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;
    }

以下是人员类别:

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。


问题答案:

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

 List<Person> records;

但是然后在你的json中

“记录”: [

它一定要是 :

"records": [

并在末尾]“},必须是

]}


 类似资料:
  • 问题内容: 我在列表中仅收到一个项目时收到此错误。我在服务器端REST Web服务中使用Jersey,当List返回一个元素并且当我得到它时,我仅收到错误,但是当它包含多个元素时,它可以正常工作,这就是我的工作方式它: 为什么Jersey不发送包含一个项目的列表?这是球衣的虫子吗?有没有办法使它始终序列化为数组? 当Web服务返回0元素时,这是我的JSON数据: 当Web服务返回1元素时,这是我的

  • 获取以下JSON的Gson解析的以下错误 11-05 15:34:00.882:w/system.err(28673):com.google.gson.jsonSyntaxException:java.lang.IllegalStateException:应为BEGIN_OBJECT,但在第1行第5923列为BEGIN_ARRAY 我正在使用belo行解析序列化上面的JSON

  • 问题内容: 我在解析json数据时遇到此错误: 我找不到解决方案。我的json数据是: 我将图像转换为字节数组,如下所示: 我正在将图标从字节数组转换为Bitmap,如下所示: 我反序列化JSON响应的代码: 这是我的ProjectContainer类: 这是Project类: 如果您能帮助我解决这个问题,我将不胜感激。提前致谢 问题答案: 当您尝试解析该字段时会引发Exception ,因为在J

  • 我的Retrofit2调用出现以下异常: 学分类别:

  • [失败:com.google.gson.jsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_ARRAY但BEGIN_OBJECT位于第1行第2列路径$] 我怎么解决?