java - 我怎样才能轻松地将 json 转换为自定义类型的 ArrayList?

标签 java android json arraylist gson

我有一个 json 字符串,我想将其转换为 ArrayList<Offer>

我尝试使用 gson 并遇到此错误:

offersList = gson.fromJson(res, ArrayList<Offer>);

Syntax error on token ">", Expression expected after this token

如何轻松地将 json 转换为自定义类型的 ArrayList?

更新

我试过@weston 的回答,部分效果不错

对于这个 json,我得到了愚蠢的数组:

[ { "address" : "my street 22, my city, my country",
    "endDateTime" : "2014-03-25T17:10:07.1466Z",
    "id" : "d8ffd4e6-6166-4fb5-bc88-10a7639bbdae",
    "latitude" : 32.457799999999999,
    "longitude" : 34.457799999999999,
    "media" : [ "0fe424fe-70cc-46fe-b" ],
    "recurringType" : 1,
    "startDateTime" : "2014-03-25T19:10:07.1466Z",
    "target" : 0,
    "title" : "some campaign title bla bla"
  },
  { "address" : "my street 22, my city, my country",
    "endDateTime" : "2014-03-25T17:10:01.749Z",
    "id" : "a860cd99-df4d-449d-b19b-9be8e65d30b1",
    "latitude" : 32.457799999999999,
    "longitude" : 34.457799999999999,
    "media" : [ "e859818f-6dbc-4801-9" ],
    "recurringType" : 1,
    "startDateTime" : "2014-03-25T19:10:01.749Z",
    "target" : 0,
    "title" : "some campaign title bla bla"
  },
  { "address" : "my street 22, my city, my country",
    "endDateTime" : "2014-03-25T17:09:57.0222Z",
    "id" : "7578c31d-477b-47c8-bf29-9f7ac39d8520",
    "latitude" : 32.457799999999999,
    "longitude" : 34.457799999999999,
    "media" : [ "a08bb80e-9522-4bb3-b" ],
    "recurringType" : 1,
    "startDateTime" : "2014-03-25T19:09:57.0222Z",
    "target" : 0,
    "title" : "some campaign title bla bla"
  },
  { "address" : "my street 22, my city, my country",
    "endDateTime" : "2014-03-25T17:09:52.1394Z",
    "id" : "47471ea4-c3d5-466f-993f-050cfe6aaa0a",
    "latitude" : 32.457799999999999,
    "longitude" : 34.457799999999999,
    "media" : [ "ac7ad715-bbb0-4649-b" ],
    "recurringType" : 1,
    "startDateTime" : "2014-03-25T19:09:52.1394Z",
    "target" : 0,
    "title" : "some campaign title bla bla"
  },
  { "address" : "my street 22, my city, my country",
    "endDateTime" : "2014-03-25T17:09:47.0226Z",
    "id" : "eceaa9ec-5cfa-4eb9-bf98-fec55d632977",
    "latitude" : 32.457799999999999,
    "longitude" : 34.457799999999999,
    "media" : [ "61c37b9d-7feb-4384-9" ],
    "recurringType" : 1,
    "startDateTime" : "2014-03-25T19:09:47.0226Z",
    "target" : 0,
    "title" : "some campaign title bla bla"
  },
  { "address" : "my street 22, my city, my country",
    "endDateTime" : "2014-03-25T17:09:41.9682Z",
    "id" : "8157b0fc-48d1-472e-9961-664a627c7676",
    "latitude" : 32.457799999999999,
    "longitude" : 34.457799999999999,
    "media" : [ "ae4fc57a-a5c4-49b7-a" ],
    "recurringType" : 1,
    "startDateTime" : "2014-03-25T19:09:41.9682Z",
    "target" : 0,
    "title" : "some campaign title bla bla"
  },
  { "address" : "my street 22, my city, my country",
    "endDateTime" : "2014-03-25T17:09:33.5286Z",
    "id" : "179299a4-97a3-4721-8e9c-ff744cb1d9a0",
    "latitude" : 32.457799999999999,
    "longitude" : 34.457799999999999,
    "media" : [ "0d74f51b-427c-4e9f-8" ],
    "recurringType" : 1,
    "startDateTime" : "2014-03-25T19:09:33.5286Z",
    "target" : 0,
    "title" : "some campaign title bla bla"
  },
  { "address" : "my street 22, my city, my country",
    "endDateTime" : "2014-03-25T17:09:28.5834Z",
    "id" : "eb23d519-a153-41ed-88d9-2ec095d77397",
    "latitude" : 32.457799999999999,
    "longitude" : 34.457799999999999,
    "media" : [ "844c1735-fe68-4d15-8" ],
    "recurringType" : 1,
    "startDateTime" : "2014-03-25T19:09:28.5834Z",
    "target" : 0,
    "title" : "some campaign title bla bla"
  },
  { "address" : "my street 22, my city, my country",
    "endDateTime" : "2014-03-25T17:09:23.8098Z",
    "id" : "03ab9a80-75cf-4b5a-8a61-d1a403105aa3",
    "latitude" : 32.457799999999999,
    "longitude" : 34.457799999999999,
    "media" : [ "a8f91f37-0181-4d95-b" ],
    "recurringType" : 1,
    "startDateTime" : "2014-03-25T19:09:23.8098Z",
    "target" : 0,
    "title" : "some campaign title bla bla"
  },
  { "address" : "my street 22, my city, my country",
    "endDateTime" : "2014-03-25T17:09:18.3186Z",
    "id" : "64bb8624-94fe-4314-8ba4-abeed832140a",
    "latitude" : 32.457799999999999,
    "longitude" : 34.457799999999999,
    "media" : [ "e34f51ff-9f70-4721-9" ],
    "recurringType" : 1,
    "startDateTime" : "2014-03-25T19:09:18.3186Z",
    "target" : 0,
    "title" : "some campaign title bla bla"
  }
]

enter image description here

我理解为什么所有字段都是空的,但是 为什么最后两个items为null?

最佳答案

你忘了放.class

offersList =  gson.fromJson(res, ArrayList<Offer>.class);

这适用于非泛型类型,但对于您需要做的集合:

Type collectionType = new TypeToken<ArrayList<Offer>>(){}.getType();
offersList = gson.fromJson(res, collectionType);

根据 https://sites.google.com/site/gson/gson-user-guide#TOC-Collections-Examples

关于java - 我怎样才能轻松地将 json 转换为自定义类型的 ArrayList?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22789608/

相关文章:

Java Paint 方法闪烁并消失

java - BebopVideoView 到 Mat

android - 从另一个应用程序 (WhatsApp) 获取路径

java - 脚本和Java : How can i say to Java to put the output file into a new folder?

java - JSON: "Unexpected character (<) at position 0"

java - 动态 Java 变量命名

java.lang.ClassCastException : oracle. sql.BLOB 无法转换为 oracle.sql.BLOB

java - 如何在java中实现获取回调的接口(interface)

android - Android 浏览器中的 HTML5 音频/视频加载问题

javascript - 使用 jquery 遍历 json 对象数组