0

I want parse all "name" and "desc" from this json object inside another json object. i need another "for"?

 {
  "main": {
    "details": [
      {
        "owner_name": "owner_name1",
        "id": "id1",
        "details2": {
          "data": [
            {
              "name": "name1",
              "desc": "my desc1",

            },
            {
              "name": "name2",
              "desc": "my desc2",

            }
          ],

        }
      },
      {
        "owner_name": "owner_name2",
        "id": "id2",
        "details2": {
          "data": [
            {
              "name": "name3",
              "desc": "my desc3",

            },
            {
              "name": "name4",
              "desc": "my desc4",

            }
          ]
        }
      }
    ]
  }
}

and My java Code is:

JSONObject jsono = new JSONObject(data);
JSONObject mainObject = jsono.getJSONObject("main");
JSONArray jsonArray = mainObject.getJSONArray("details");

for (int i = 0; i < jsonArray.length(); i++) {
    JSONObject object1 = jsonArray.getJSONObject(i);

     JSONObject objectDetails2 = object1.getJSONObject("details2");
     JSONArray jsonArrayData = objectDetails2.getJSONArray("data");

   for (int j = 0; j < jsonArrayData.length(); j++) {
         JSONObject object = jsonArrayData.getJSONObject(j);   
         Actors actor = new Actors();
    actor.setName(object.getString("name"));
    actor.setDesc(object.getString("desc"));


    actorsList.add(actor);

      }

It only show First "data" result inside details2 not showing second details2 inside data.. Now my result is: "name1,dmy desc1,name2,my desc2"

i want all result like : "name1,dmy desc1,name2,my desc2,name3,my desc3,name4,my desc4"

0

3 Answers 3

3

How can i go again inside object and array? i need details from "name" and "desc"

Get details2 from object JSONObject and then get data JSONArray:

 for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject object = jsonArray.getJSONObject(i);
         // get details2 JSONObject
         if(object.has("details2")){
           if(!object.isNUll("details2")){
            JSONObject objectDetails2 = object.getJSONObject("details2");
            // get data JSONArray from objectDetails2
            if(objectDetails2.has("data")){
              if(!objectDetails2.isNUll("data")){
                JSONArray jsonArrayData = objectDetails2.getJSONArray("data");
                 // iterate to jsonArrayData 
                 for (int j = 0; j < jsonArrayData.length(); j++) {
                    JSONObject objectInner = jsonArray.getJSONObject(j);
                    String strName=objectInner.optString("name");
                    ....  
                  }
        }else{
                 // details2 found but null
                }
            }else{
            // details2 not found
            }
         }else{
           // details2 found but null
        }
       }else{
           // details2 not found
       }
}
Sign up to request clarification or add additional context in comments.

3 Comments

@Mullinsangebn: debug your code somewhere is breaking after reading first object
Yes one array dont have details2 and data.. but next one have it... how can i fix it?
@Mullinsangebn: ok use isNull and has method for checking is JSONObject or JSONArray contain details2 or data if contain then parse other-wise move to next
0

I think you should go into "details2" JSON Object like this :

JSONArray jsonArrayData=object.getJSONObject("details2").getJSONArray("data");

Comments

0

you have to get the object first and then parse it for the diffrent names and info

JSONArray jsonArrayData;
  for (int i = 0; i < jsonArray.length(); i++) {
    JSONObject object = jsonArray.getJSONObject("details");
    jsonArrayData=object.getJSONArray("data");
    for(int k=0;k<jsonArrayData.length();k++)
      {
    Actors actor = new Actors();
    actor.setName(object.getString("name"));
    actor.setDesc(object.getString("desc"));        
    actorsList.add(actor);
   }
 }

i will suggest you use GSON it is quite simple to use,

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.