GSON抛出“预期的BEGIN_Object但是BEGIN_数组”?

GSON抛出“预期的BEGIN_Object但是BEGIN_数组”?

我正试图解析像这样的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);

我该怎么解决呢?


温温酱
浏览 1223回答 3
3回答

胡说叔叔

问题是,您要求的是一个类型的对象。ChannelSearchEnum但你真正拥有的是一个类型的对象List<ChannelSearchEnum>.您可以通过以下方式实现这一点:Type&nbsp;collectionType&nbsp;=&nbsp;new&nbsp;TypeToken<List<ChannelSearchEnum>>(){}.getType();List<ChannelSearchEnum>&nbsp;lcs&nbsp;=&nbsp;(List<ChannelSearchEnum>)&nbsp;new&nbsp;Gson() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.fromJson(&nbsp;jstring&nbsp;,&nbsp;collectionType);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java