【问题标题】:how can i easily convert the json to such ArrayList Of custom type?我怎样才能轻松地将 json 转换为这种 ArrayList 的自定义类型?
【发布时间】:2014-05-12 10:43:29
【问题描述】:

我有一个 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"
  }
]

我明白为什么所有字段都是空的,但是 为什么最后两个 item 为 null?

【问题讨论】:

  • 解析 JSON 字符串并在循环时创建一个新的 Offer 对象

标签: java android json arraylist gson


【解决方案1】:

你忘记输入.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

【讨论】:

  • 根据我的经验,第二个建议是实际的解决方案
  • 请查看我更新的问题。为什么最后两个为空?
  • @EladBenda ArrayList 中的底层数组通常大于size,即10,这是正确的。这样您就可以添加到列表中,而无需经常调整此底层数组的大小。所以索引[10][11] 是无效的,你不能通过调用offersList.get(10) 来访问它们。
【解决方案2】:

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

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

【讨论】:

  • ` Android API 自带的`你能举个例子吗?这对我来说不是很清楚
  • 请看我的新答案,看看如何使用JSONArrayJSONObject
  • 在某些时候你应该学习如何使用链接!那为什么不现在呢?只需这样做:[此处的文字](此处的链接)
【解决方案3】:

关注此代码

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() 的更多信息

【讨论】:

  • 您为什么不将此添加到您的其他答案中?
猜你喜欢
  • 1970-01-01
  • 2013-10-17
  • 1970-01-01
  • 2012-08-24
  • 1970-01-01
  • 1970-01-01
  • 2022-01-24
  • 2022-11-29
  • 1970-01-01
相关资源
最近更新 更多