GSON 抛出“预期为 BEGIN_OBJECT 但为 BEGIN_ARRAY"? [英] GSON throwing "Expected BEGIN_OBJECT but was BEGIN_ARRAY"?

查看:31
本文介绍了GSON 抛出“预期为 BEGIN_OBJECT 但为 BEGIN_ARRAY"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析这样的 JSON 字符串

<预><代码>[{"updated_at":"2012-03-02 21:06:01","fetched_at":"2012-03-02 21:28:37.728840",说明":空,语言":空,"title":"约翰","url":"http://rus.JOHN.JOHN/rss.php",icon_url":空,logo_url":空,"id":"4f4791da203d0c2d76000035",修改":2012-03-02 23:28:58.840076"},{"updated_at":"2012-03-02 14:07:44","fetched_at":"2012-03-02 21:28:37.033108",说明":空,语言":空,"title":"彼得","url":"http://PETER.PETER.lv/rss.php",icon_url":空,logo_url":空,"id":"4f476f61203d0c2d89000253",修改":2012-03-02 23:28:57.928001"}]

进入对象列表.

Listlcs = (List) new Gson().fromJson( jstring , ChannelSearchEnum.class);

这是我正在使用的对象类.

import com.google.gson.annotations.SerializedName;公共类 ChannelSearchEnum {@SerializedName("updated_at")私人字符串updated_at;@SerializedName("fetched_at")私人字符串fetched_at;@SerializedName("描述")私人字符串描述;@SerializedName("语言")私有字符串语言;@SerializedName("标题")私人字符串标题;@SerializedName("url")私人字符串网址;@SerializedName("icon_url")私人字符串 icon_url;@SerializedName("logo_url")私人字符串 logo_url;@SerializedName("id")私人字符串ID;@SerializedName("修改")私有字符串已修改;公共最终字符串 get_Updated_at() {返回 this.updated_at;}公共最终字符串 get_Fetched_at() {返回 this.fetched_at;}公共最终字符串 get_Description() {返回 this.description;}公共最终字符串 get_Language() {返回 this.language;}公共最终字符串 get_Title() {返回 this.title;}公共最终字符串 get_Url() {返回 this.url;}公共最终字符串 get_Icon_url() {返回 this.icon_url;}公共最终字符串 get_Logo_url() {返回 this.logo_url;}公共最终字符串 get_Id() {返回 this.id;}公共最终字符串 get_Modified() {返回 this.modified;}}

但它让我觉得

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: 预期为 BEGIN_OBJECT 但在第 1 行第 2 列是 BEGIN_ARRAY

任何想法我应该如何解决它?

解决方案

问题是你告诉 Gson 你有一个你的类型的对象.你没有.你有一个你类型的对象数组.你不能只是尝试像那样投射结果并期望它神奇地工作;)

Gson 的用户指南解释了如何处理:

https://github.com/google/gson/blob/master/用户指南.md

这会起作用:

ChannelSearchEnum[] enums = gson.fromJson(yourJson, ChannelSearchEnum[].class);

但这更好:

Type collectionType = new TypeToken>(){}.getType();集合枚举 = gson.fromJson(yourJson, collectionType);

I'm trying to parse a JSON string like this one

[
   {
      "updated_at":"2012-03-02 21:06:01",
      "fetched_at":"2012-03-02 21:28:37.728840",
      "description":null,
      "language":null,
      "title":"JOHN",
      "url":"http://rus.JOHN.JOHN/rss.php",
      "icon_url":null,
      "logo_url":null,
      "id":"4f4791da203d0c2d76000035",
      "modified":"2012-03-02 23:28:58.840076"
   },
   {
      "updated_at":"2012-03-02 14:07:44",
      "fetched_at":"2012-03-02 21:28:37.033108",
      "description":null,
      "language":null,
      "title":"PETER",
      "url":"http://PETER.PETER.lv/rss.php",
      "icon_url":null,
      "logo_url":null,
      "id":"4f476f61203d0c2d89000253",
      "modified":"2012-03-02 23:28:57.928001"
   }
]

into a list of objects.

List<ChannelSearchEnum> lcs = (List<ChannelSearchEnum>) new Gson().fromJson( jstring , ChannelSearchEnum.class);

Here's an object class I'm using.

import com.google.gson.annotations.SerializedName;

public class ChannelSearchEnum {



@SerializedName("updated_at")
private String updated_at;

@SerializedName("fetched_at")
private String fetched_at;

@SerializedName("description")
private String description;

@SerializedName("language")
private String language;

@SerializedName("title")
private String title;

@SerializedName("url")
private String url;

@SerializedName("icon_url")
private String icon_url;

@SerializedName("logo_url")
private String logo_url;

@SerializedName("id")
private String id;

@SerializedName("modified")
private String modified;

public final String get_Updated_at() {
    return this.updated_at;
}

public final String get_Fetched_at() {
    return this.fetched_at;
}

public final String get_Description() {
    return this.description;
}

public final String get_Language() {
    return this.language;
}

public final String get_Title() {
    return this.title;
}

public final String get_Url() {
    return this.url;
}

public final String get_Icon_url() {
    return this.icon_url;
}

public final String get_Logo_url() {
    return this.logo_url;
}

public final String get_Id() {
    return this.id;
}

public final String get_Modified() {
    return this.modified;
}

        }

But it throws me with

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2

Any ideas how should I fix it?

解决方案

The problem is you're telling Gson you have an object of your type. You don't. You have an array of objects of your type. You can't just try and cast the result like that and expect it to magically work ;)

The User guide for Gson Explains how to deal with this:

https://github.com/google/gson/blob/master/UserGuide.md

This will work:

ChannelSearchEnum[] enums = gson.fromJson(yourJson, ChannelSearchEnum[].class);

But this is better:

Type collectionType = new TypeToken<Collection<ChannelSearchEnum>>(){}.getType();
Collection<ChannelSearchEnum> enums = gson.fromJson(yourJson, collectionType);

这篇关于GSON 抛出“预期为 BEGIN_OBJECT 但为 BEGIN_ARRAY"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆