Mullinsangebn
Mullinsangebn

Reputation: 133

Android Parsing JSON object inside another object

I try to Parsing JSON object inside another object in Android.

{
  "data": [
    {
      "id": "1", 
      "picture": "http://mp3dow.com/images/logo.png", 
      "from": {
        "id": "1", 
        "name": "name1"
            }
    }, 

    {
      "id": "2", 
      "picture": "http://mp3dow.com/images/logo.png", 
      "from": {
        "id": "2", 
        "name": "name2"
            } 
    }, 

    {
      "id": "3", 
      "picture": "http://mp3dow.com/images/logo.png", 
      "from": {
        "id": "3", 
        "name": "name3"
            }
    }
  ]
}

and my Android code

//Oncreate

new JSONAsyncTask().execute("http://bomaza.com/test/json.php");

//doInBackground

@Override
protected Boolean doInBackground(String... urls) {
    try {
        HttpGet httppost = new HttpGet(urls[0]);
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response = httpclient.execute(httppost);
        int status = response.getStatusLine().getStatusCode();

        if (status == 200) {
            HttpEntity entity = response.getEntity();
            String data = EntityUtils.toString(entity);

            JSONObject jsono = new JSONObject(data);
            JSONArray jarray = jsono.getJSONArray("data");

            for (int i = 0; i < jarray.length(); i++) {
                JSONObject object = jarray.getJSONObject(i);

                Actors actor = new Actors();

                actor.setName(object.getString("id"));
                actor.setDesc(object.getString("name"));
                actor.setImage(object.getString("picture"));

                actorsList.add(actor);
            }
            return true;
        }

        //------------------>>

    } catch (ParseException e1) {
        e1.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return false;
}

I have problem with Child object parsing "name" filed not parsing how can i fix it? Inside object ("from") not parsing.

I want parse both "picture", and "name".

Upvotes: 0

Views: 707

Answers (1)

Bhavin Shah
Bhavin Shah

Reputation: 361

Try this code. Hope its works for you.

Actors actor = new Actors();
actor.setName(object.getString("id"));
actor.setImage(object.getString("picture"));  
JSONObject object1 = object.getJSONObject("from");
actor.setDesc(object1.getString("name"));

Upvotes: 1

Related Questions