从JSONObject获取Index的JSONArray

时间:2014-01-05 12:18:19

标签: android json

我有一个带有多个JSONArrays的JSONObject。我已经编写了一个for循环来遍历对象,但我需要在Index位置获取JSONArray。有谁知道怎么做?

继承我的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.. 
      }
} 

继承我的for循环这个问题我有的是从JSONObject检索JSONArray它需要一个字符串,但我试图在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 个答案:

答案 0 :(得分:4)

试试这个:

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

请注意,JSONObject元素的顺序定义。

答案 1 :(得分:0)

 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;
                }
            }