java–GSON投掷“预期BEGIN_OBJECT但是BEGIN_ARRAY”?

java–GSON投掷“预期BEGIN_OBJECT但是BEGIN_ARRAY”?,第1张

概述我正在尝试解析像这样的JSON字符串[{"updated_at":"2012-03-0221:06:01","fetched_at":"2012-03-0221:28:37.728840","description":null,"language":null,"title":&quo

我正在尝试解析像这样的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投掷预期BEGIN_OBJECT但是BEGIN_ARRAY”?全部内容,希望文章能够帮你解决java – GSON投掷“预期BEGIN_OBJECT但是BEGIN_ARRAY”?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1104532.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存