我有这样的代码,jArrAnswer
的值是
[{"answer":"Yes"},{"answer":"No"},{"answer":"maybe"},{"answer":"yrg"}]
jArrAnswer.length()
的结果是4
但为什么我收到错误
org.json.JSONException:索引1超出范围[0..1)。
try {
JSONArray jArrAnswerid = new JSONArray(answerid);
JSONArray jArrAnswer = new JSONArray(answer);
for (int i = 0; i < jArrAnswer.length(); i++) {
JSONObject jObjAnswerid = jArrAnswerid.getJSONObject(i);
JSONObject jObjAnswer = jArrAnswer.getJSONObject(i);
String ansid = jObjAnswerid.getString("answerid");
String ans= jObjAnswer.getString("answer");
GroupModel item2 = new GroupModel(String.valueOf(i + 1), ans, ansid);
}
} catch (Exception e) {
Log.w("asdf", e.toString());
}
答案 0 :(得分:5)
当您将索引for loop
提取到jArrAnswer
时,您正在i
重复jArrAnswerid
。
检查并确保jArrAnswerid.size()
<{1}}与<{1}} 相等。
打印jArrAnswer.size()
并检查。
答案 1 :(得分:2)
试试这个
$queryold="SELECT * FROM tablename order by STR_TO_DATE(column_name,'%d/%m/%Y')";
提供“answer”是你的json数组响应
答案 2 :(得分:0)
尝试
String json = "[{\"answer\":\"Yes\",\"answerid\":\"1\"},{\"answer\":\"No\",\"answerid\":\"2\"},{\"answer\":\"maybe\",\"answerid\":\"3\"},{\"answer\":\"yrg\",\"answerid\":\"4\"}]";
try {
JSONArray jsonArray = new JSONArray(json);
if(jsonArray != null) {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String answerId = jsonObject.getString("answerid");
String answer = jsonObject.getString("answer");
//Use answerId and answer
}
}
} catch(JSONException e) {
e.printStackTrace();
}