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

时间:2014-04-01 14:58:18

标签: 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的答案而且它在paritally工作

对于这个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

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

3 个答案:

答案 0 :(得分:6)

您忘了放.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

答案 1 :(得分:0)

或者,如果您不想使用Google的库,则可以使用Android API附带的Apache库。使用http://developer.android.com/reference/org/json/JSONArray.html(JSONArray)和http://developer.android.com/reference/org/json/JSONObject.html(JSONObject)

(请对此进行编辑,我不知道如何添加链接;))

答案 2 :(得分:0)

请遵循此代码

String strJSON = ......;
JSONArray arr = new JSONArray(strJSON);
for(int i = 0; i < arr.length(); i++) {
    JSONObject job = arr.getJSONObject(i);
    //use job.getString() job.getInt(), etc to retrieve data and add to adapter
}

您可以从我之前回答中提供的链接中找到有关job.getString()job.getInt()的更多信息