Android 从JSONObject获取索引处的JSONArray
我有一个JSONObject,其中包含多个JSONArray。我已经编写了一个for循环来循环对象,但是我需要在索引位置获取JSONArray。有人知道怎么做吗 这是我的JSONObjectAndroid 从JSONObject获取索引处的JSONArray,android,json,Android,Json,我有一个JSONObject,其中包含多个JSONArray。我已经编写了一个for循环来循环对象,但是我需要在索引位置获取JSONArray。有人知道怎么做吗 这是我的JSONObject {"Contacts": //JSONObject { "B"://JSONArray.. [ {"ContactName":sdfsdf,"ID":900,"Number":1368349}, {"ContactName":adsdfd,"ID":19
{"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..
}
}
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););
}
这里是我的循环,我遇到的问题是,要从JSONObject检索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..
}
}
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););
}
JSONArray headerStrings=contacts.names();
Log.v(“Main”、“headerStrings=“+headerStrings”);
SeparatedListAdapter=新的SeparatedListAdapter(此);
对于(int t=0;t试试这个:
for (Iterator it = contacts.keys(); it.hasNext(); ) {
String name = (String)it.next();
JSONArray arr = contacts.optJSONArray(name);
// now add this to your adapter
}
请注意,JSONObject元素的顺序没有定义。请尝试以下操作:
for (Iterator it = contacts.keys(); it.hasNext(); ) {
String name = (String)it.next();
JSONArray arr = contacts.optJSONArray(name);
// now add this to your adapter
}
注意,JSONObject元素的顺序没有定义。这里,您将得到匹配的索引
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;
}
}
int matchedIndex=0;
对于(inti=0;i这里,您将得到匹配的索引
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;
}
}
int matchedIndex=0;
对于(int i=0;i