Reputation: 903
i'm trying to parse Json with the google Gson library, but for the moment i can't get it to work ...
Here is the Json i'm supposed to get :
{
"shoppingLists": [
{
"ShoppingList": {
"id": "51",
"name": "loool",
"created": "2014-03-22 13:03:22",
"modified": "2014-03-22 13:03:22"
},
"ShoppingItem": [
{
"id": "24",
"shopping_item_category_id": "19",
"name": "Biscuits",
"description": "",
"created": "2014-02-05 17:43:45",
"modified": "2014-02-05 17:43:45",
"category_name": "Confiseries \/ Gouters"
},
{
"id": "25",
"shopping_item_category_id": "19",
"name": "Snickers",
"description": "",
"created": "2014-02-05 17:44:08",
"modified": "2014-02-05 17:44:08",
"category_name": "Confiseries \/ Gouters"
},
{
"id": "26",
"shopping_item_category_id": "19",
"name": "C\u00e9reales",
"description": "",
"created": "2014-02-05 17:44:57",
"modified": "2014-02-05 17:44:57",
"category_name": "Confiseries \/ Gouters"
}
]
}
]
}
Here are my models :
shoppingLists :
public class shoppingLists {
public ShoppingList ShoppingList;
public List<ShoppingItem> ShoppingItems;
}
ShoppingList :
public class ShoppingList {
public int id;
public String name;
public String created;
public String modified;
}
ShoppingItem :
public class ShoppingItem {
public int id;
public int shopping_item_category_id;
public String name;
public String description;
public String created;
public String modified;
public String category_name;
}
Here is my AsyncTask which get the Json from the server :
public class APIRetrieveLists extends AsyncTask<APIRequestModel, Void, List<shoppingLists>>{
SQLHelper _sqlHelper = null;
Context _context;
ProgressBar _pb;
public APIRetrieveLists(Context context, ProgressBar pb){
this._context = context;
this._pb = pb;
}
@Override
protected void onPreExecute(){
}
@Override
protected void onPostExecute(List<shoppingLists> model){
this._sqlHelper = new SQLHelper(this._context);
if (model != null){
for (shoppingLists cn : model){
Log.i("infos", "list's name => " + cn.ShoppingList.name);
}
}else{
Log.i("infos", "model is null");
}
}
@Override
protected List<shoppingLists> doInBackground(APIRequestModel... arg0) {
APIRequestModel model = arg0[0];
try
{
try
{
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("access_token", model.getToken()));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://apiurl/index.json");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
HttpResponse response = httpclient.execute(httppost);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String s = "";
String all = "";
while ((s = reader.readLine()) != null){
all += s;
}
Gson gson = new Gson();
List<shoppingLists> Obj = (List<shoppingLists>) gson.fromJson(all, new TypeToken<List<shoppingLists>>(){}.getType());
return (Obj);
}
catch (ClientProtocolException e) {
Log.i("infos", "first");
return (null);
}
}
catch (Exception e){
Log.i("infos", "second");
return (null);
}
}
}
The exception "second" is always firing ... Log.i("infos", "model is null"); is also executed. if someone could help :) thank you ! regards.
Upvotes: 1
Views: 1006
Reputation: 3820
Add one more wrapper to your shoppingLists, as your jsonObject had shoppingLists as key and array of ShoppingListItem as value. Also you need to annotate @SerializedName
for ShoppingItems field because it is not matching with the key in Json.
Find below updated classes and parser
Gson gson = new Gson();
FinalClass myObj = gson.fromJson(jsonString, FinalClass.class);
Classes
public class FinalClass {
public ArrayList<ShoppingListItem> shoppingLists;
}
public class ShoppingListItem {
public ShoppingList ShoppingList;
@SerializedName("ShoppingItem")
public List<ShoppingItem> ShoppingItems;
}
public class ShoppingList {
public int id;
public String name;
public String created;
public String modified;
}
public class ShoppingItem {
public int id;
public int shopping_item_category_id;
public String name;
public String description;
public String created;
public String modified;
public String category_name;
}
Upvotes: 2
Reputation: 14710
You don't have a list of shoppingLists
items in the json - if my glasses don't fool me. So changing:
List<shoppingLists> Obj = (List<shoppingLists>) gson.fromJson(all, new TypeToken<List<shoppingLists>>(){}.getType());
into:
shoppingLists obj = gson.fromJson(all, shoppingLists.class);
will not cause any runtime gson exceptions. Eventually you can wrap that single parsed obj
into a List
so it matches with your current code. For Example:
shoppingLists obj = gson.fromJson(all, shoppingLists.class);
ArrayList<shoppingLists> result = new ArrayList<shoppingLists>();
result.add(obj);
return result;
Upvotes: 1