0

I obtain a JSONObject from my server in my Android application through a service and in order to send to my activity I convert JSONObject to a string with

String myjson= gson.toJson(object);
b.putString("json", myjson);

And transfer the string to my activity where I recreate the JSONObject from String

Gson gson = new Gson();
JSONObject jobj = gson.fromJson(mydata, JSONObject.class);
JSONArray jarray = jobj.getJSONArray("myitems");

My JSON is as below

{"myitems":[{"event_id":"1","title":"Music launch party at makeover","description":"Music Launch function at Makeover","store_id":"2","user_id":"1","category_id":"1","submittedby":"store","start_date":"2015-02-03 09:00:01","end_date":"2015-02-03 20:00:00","price":"1000","gallery_1":"","gallery_2":"","gallery_3":"","add_lat":"30.693771","add_lon":"76.76486","distance":"10.329089177806534"},{"event_id":"2","title":"The Bacardi party at the New year bash","description":"Altius organizes a new year bash at their Night House.","store_id":"2","user_id":"1","category_id":"3","submittedby":"user","start_date":"2015-02-05 17:08:40","end_date":"2015-02-05 22:08:48","price":"2000","gallery_1":"","gallery_2":"","gallery_3":"","add_lat":"30.69461","add_lon":"76.76176","distance":"10.575941394542852"}]}

But I am getting error on getting jsonarray from the jsonobject. what should be the right way to obtain the jsonarray from the jsonobject or what wrong I am doing here.

3
  • what does the error say? Commented Feb 5, 2015 at 16:24
  • jsonexception .gson.internal.LinkedTreeMap cannot be converted to jsonarray Commented Feb 5, 2015 at 16:28
  • 1
    Here a bit of advice which i got from net while searching JSON manipulation that when working with nested Objects or array in JSON try to make mapper class properly then do your work. Commented Feb 5, 2015 at 16:37

1 Answer 1

2

Try the following :

  1. Create a class containing a replica of the json objects

    public class MyItem {

    String event_id; String title; String description; String store_id ; String user_id; String category_id; String submittedby; String start_date; String end_date; String price; String gallery_1; String gallery_2; String gallery_3; String add_lat; String add_lon; String distance;

    public MyItem() { } }

  2. In your case myjson is the string containing the json reply..so

            JSONArray array = null;
    
            try {
                JSONObject jsonResp = new JSONObject(myjson);
                array = jsonResp.getJSONArray("myitems");
    
            } catch (JSONException e) {
    
                e.printStackTrace();
            }
    
  3. Then you can iterate through the array and store objects in your custom class like so :

for(int i = 0; i < array.length(); i++){ JSONObject json2 = null;

try { json2 = (JSONObject) array.get(i); } catch (JSONException e) { e.printStackTrace(); } Gson gson = new Gson(); MyItem myItem = gson.fromJson(json2.toString(), MyItem.class); //Do whatever you want with the object here

}

Sign up to request clarification or add additional context in comments.

8 Comments

if i am not using gson to convert string back to jsonobject and do as you have written i get error in getting jsonarray that no value for myitems.kindly update
its because you are not mapping the json object into a custom class...try my example it works...i tested it
this is logcat of title field : myITEM : Music launch party at makeover
Plus one from my side because that is a legit approach towards json handling and that's what i said above .!
true i just noticed that :D
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.