java - GSON 抛出 "Expected BEGIN_OBJECT but was BEGIN_ARRAY"?

标签 java android gson

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

[
   {
      "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"
   }
]

到对象列表中。

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

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

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

        }

但它让我感到震惊

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

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

最佳答案

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

Gson 的用户指南解释了如何处理这个问题:

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

这将起作用:

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

但这样更好:

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

关于java - GSON 抛出 "Expected BEGIN_OBJECT but was BEGIN_ARRAY"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9598707/

相关文章:

android - 当来自原生 android 方法 channel 的回调时,在 dart 中运行方法或函数

android - Jetpack Compose 中的作用域状态

java - gson排除策略仅适用于目标对象的字段

android - 通过 JSON 从 Android 到 ASP.NET 网络服务的用户定义对象列表

java - 如何使用 Eclipse 删除未使用的导入而不重新组织它?

java - 对 Date 对象进行空检查

android - 如何在android中直接打开Gmail邮件编辑器?

java - JPA+GSON : Queries result in Stackoverflow only after server restart

java - 按字符对数组中的字符串进行排序

java - 切换大于/小于