0

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".

1
  • I would recommend looking into GSON and subclassing. Commented Apr 13, 2016 at 14:20

1 Answer 1

1

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"));
Sign up to request clarification or add additional context in comments.

Comments

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.