0

I have a JSONObject with multiple JSONArrays in it. I have written a for loop to loop through the object but i need to get the JSONArray at the Index position. Does anyone know how to do this?

heres my JSONObject

{"Contacts": //JSONObject
  {
    "B"://JSONArray..
    [
        {"ContactName":sdfsdf,"ID":900,"Number":1368349}, 
        {"ContactName":adsdfd,"ID":1900,"Number":136856},  
         {"ContactName":adglkhdofg,"ID":600,"Number":136845}
   ],
  "C":[
         {"ContactName":alkghoi,"ID":900,"Number":1368349},
         {"ContactName":wetete,"ID":1900,"Number":136856}, 
         {"ContactName":dfhtfh,"ID":600,"Number":136845}
     ]
      .....//and so on.. 
      }
} 

heres my for loop this issue i'm having is that to retrieve a JSONArray from a JSONObject it requires a string but i'm trying to get the Array at object Index in the JSONObject

JSONArray headerStrings = contacts.names();
                    Log.v("Main", "headerStrings = " + headerStrings);

                    SeparatedListAdapter adapter = new SeparatedListAdapter(this);  

                    for (int t=0; t<contacts.length(); t++){

                    adapter.addSection(headerStrings.getString(t), new DocumentArrayAdapter (getActivity(),R.layout.document_cell,contacts.getJSONArray(t););   

                    }

2 Answers 2

4

Try this:

for (Iterator it = contacts.keys(); it.hasNext(); ) {
    String name = (String)it.next();
    JSONArray arr = contacts.optJSONArray(name);
    // now add this to your adapter
}

Note, that the order of the elements of a JSONObject is not defined.

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

Comments

1

Here, You will get matched index.

 int matchedIndex =0;
 for(int i=0;i<jsonArray.length();i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                if( 123 == jsonObject.getInt("Id")){
                    matchedIndex = i;
                    //jsonArraySelectedBikes.remove(matchedIndex);
                    break;
                }
            } 

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.