android JSONException index 1 out of range [0..1] (Parse 2 json arrays inside 1 loop)

前端 未结 3 1880
失恋的感觉
失恋的感觉 2021-01-29 10:24

I have code like this, the value of jArrAnswer is

[{\"answer\":\"Yes\"},{\"answer\":\"No\"},{\"answer\":\"maybe\"},{\"answer\":\"yrg\"}]

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-29 11:12

    Try this

    try {
            JSONArray jArrAnswer = new JSONArray(answer);
            for (int i = 0; i < jArrAnswer.length(); i++) {
                JSONObject jObjAnswer = jArrAnswer.getJSONObject(i);
                String ansid = jObjAnswer.getString("answerid");
                String ans= jObjAnswer.getString("answer");
            }
         } catch (Exception e) {
            Log.w("asdf", e.toString());
         }
    

    provided "answer" is your json array response

提交回复
热议问题