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

问题描述

我试图分析这样一个JSON字符串

  [
   {
      的updated_at:2012-03-02 21时06分○一秒,
      fetched_at:2012-03-02 21:28:37.728840,
      说明:空,
      语言:空,
      头衔:约翰,
      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十四点07分44秒,
      fetched_at:2012-03-02 21:28:37.033108,
      说明:空,
      语言:空,
      头衔:彼得,
      URL:http://PETER.PETER.lv/rss.php
      icon_url:空,
      logo_url:空,
      ID:4f476f61203d0c2d89000253
      修改:2012-03-02 23:28:57.928001
   }
]
 

成的对象的列表。

 名单,其中,channelSearchEnum> LCS =(名单< channelSearchEnum>)新GSON()fromJson(的jstring,channelSearchEnum.class)。
 

下面是我使用的一个对象类。

 进口com.google.gson.annotations.SerializedName;

公共类channelSearchEnum {



@SerializedName(的updated_at)
私人字符串的updated_at;

@SerializedName(fetched_at)
私人字符串fetched_at;

@SerializedName(说明)
私人字符串描述;

@SerializedName(语言)
私人字符串的语言;

@SerializedName(标题)
私人字符串名称;

@SerializedName(URL)
私人字符串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但BEGIN_ARRAY位于第1行第2列
 

我应该怎么解决这个问题的任何想法?

谢谢!

编辑:感谢您的回信。得到它的工作的方式。

  GSON GSON =新GSON();
    JsonParser分析器=新JsonParser();
    JsonArray jArray = parser.parse(的jstring).getAsJsonArray();

    ArrayList的< channelSearchEnum> LCS =新的ArrayList< channelSearchEnum>();

    对于(JsonElement OBJ:jArray)
    {
        channelSearchEnum CSE = gson.fromJson(OBJ,channelSearchEnum.class);
        lcs.add(CSE);
    }
 

解决方案

现在的问题是你告诉 GSON 你有你的类型的对象。你不知道。你有你的类型的对象的数组。你不能只是尝试,并把结果一样,并期望它能够奇迹般地工作;)

用户指南 GSON 说明如何处理这样的:

https://sites.google.com/site/gson/gson-user-guide

这将工作:

  channelSearchEnum []枚举= gson.fromJson(yourJson,channelSearchEnum []类。);
 

但是,这是更好的:

 键入collectionType =新TypeToken<收集和LT; channelSearchEnum>>(){}的getType();
收藏< channelSearchEnum>枚举= 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?

Thanks!

EDIT :Thanks for your replys. Got it working that way.

    Gson gson = new Gson();
    JsonParser parser = new JsonParser();
    JsonArray jArray = parser.parse(jstring).getAsJsonArray();

    ArrayList<channelSearchEnum> lcs = new ArrayList<channelSearchEnum>();

    for(JsonElement obj : jArray )
    {
        channelSearchEnum cse = gson.fromJson( obj , channelSearchEnum.class);
        lcs.add(cse);
    }
解决方案

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://sites.google.com/site/gson/gson-user-guide

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投掷&QUOT;预期BEGIN_OBJECT但BEGIN_ARRAY&QUOT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 11:31