我有一个jArray和来自该jArray的字符串," message2":
[{"date":"2012","count":"1","message" : "message1"}, {"date":"2011","count":"2","message":"message2"}}
我怎样才能确定哪个JSONObject" message2"来自然后确定"计数"与那条消息有关吗?
伪代码:
x = count of jArray element that contains the string "message2"
答案 0 :(得分:0)
如果JSONArray未排序,这是基本方法:
try {
//JSONArray jArray = new JSONArray();
JSONObject item;
boolean found = false;
int length = jArray.length();
String key = "message";
String orphan = "message2"; // Let's find where you belong
for(int index = 0; index < length; index++) {
item = jArray.getJSONObject(index);
if(item.getString(key).equals(orphan)) {
found = true;
break;
}
}
if(found) {
// item references the JSONObject that you want
}
else {
// No match found
}
}
catch(JSONException e) {
// Try to handle the error gracefully
}