Reputation: 36664
I have a json string which i want to convert to ArrayList<Offer>
I have tried to use gson and got this error:
offersList = gson.fromJson(res, ArrayList<Offer>);
Syntax error on token ">", Expression expected after this token
how can i easily convert the json to such ArrayList Of custom type?
update
I have tried @weston answer and it worked paritally
for this json, i got the foolowing array:
[ { "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"
}
]
I understand why all fields are null, but why are the two items last ones null?
Upvotes: 1
Views: 147
Reputation: 54801
You forgot to put .class
offersList = gson.fromJson(res, ArrayList<Offer>.class);
Which would work for non-generic types, but for collections you need to do:
Type collectionType = new TypeToken<ArrayList<Offer>>(){}.getType();
offersList = gson.fromJson(res, collectionType);
According to https://sites.google.com/site/gson/gson-user-guide#TOC-Collections-Examples
Upvotes: 6
Reputation: 1360
Follow this code
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
}
You can find more info on job.getString()
and job.getInt()
from the links I gave in my earlier answer
Upvotes: 0
Reputation: 1360
Or, if you don't want to use Google's library, you can use Apache's library which comes with Android API. Use http://developer.android.com/reference/org/json/JSONArray.html (JSONArray) and http://developer.android.com/reference/org/json/JSONObject.html (JSONObject)
(Please make edits to this, I don't know how to add links ;))
Upvotes: 0