android - JSON Parsing Error on Looping and others -
i having hard time parsing through following json output:
{ "status":"success", "data":[ { "question_id":"1", "category_id":"1", "question":"jjj", "answer":"ffdf", "created_on":"16 apr, 2013 06:52", "modified_on":"", "user_id":"1", "category_name":"career", "is_answered":true }, { "question_id":"3", "category_id":"1", "question":"ssssssssssss", "answer":null, "created_on":"23 apr, 2013 15:12", "modified_on":"", "user_id":"1", "category_name":"career", "is_answered":false } ], "message":null }
the code have tried is:
jsonobject getquestion; public class getquestionanswers extends asynctask<void, void, void> { progressdialog progress; @override protected void onpreexecute() { super.onpreexecute(); progress = new progressdialog(questionstack.this); progress.setprogressstyle(progressdialog.style_spinner); progress.setmessage("updating information ..."); progress.show(); } @override protected void doinbackground(void... params) { userfunctions users = new userfunctions(); getquestion = users.getallquestion(question_user_id); return null; } @override protected void onpostexecute(void result) { try { jsonarray data = getquestion.getjsonarray("data"); log.i("data", data.tostring()); (int = 0; < data.length(); i++) { jsonobject goaccess = data.getjsonobject(i); question_json = goaccess.getstring("question"); answer_json = goaccess.getstring("answer"); log.i("question", question_json); log.i("answer", answer_json); question_answer_view.settext(question_json + answer_json); } } catch (jsonexception e) { e.printstacktrace(); } progress.dismiss(); super.onpostexecute(result); } }
the error shows:
04-25 18:54:26.234: w/system.err(2945): @ org.json.json.typemismatch(json.java:100) 04-25 18:54:26.234: w/system.err(2945): @ org.json.jsonobject.getjsonarray(jsonobject.java:548) 04-25 18:54:26.244: w/system.err(2945): @ com.example.btshome.questionstack$getquestionanswers.onpostexecute(questionstack.java:80) 04-25 18:54:26.244: w/system.err(2945): @ com.example.btshome.questionstack$getquestionanswers.onpostexecute(questionstack.java:1) 04-25 18:54:26.244: w/system.err(2945): @ android.os.asynctask.finish(asynctask.java:631) 04-25 18:54:26.244: w/system.err(2945): @ android.os.asynctask.access$600(asynctask.java:177) 04-25 18:54:26.264: w/system.err(2945): @ android.os.asynctask$internalhandler.handlemessage(asynctask.java:644) 04-25 18:54:26.304: w/system.err(2945): @ android.os.handler.dispatchmessage(handler.java:99) 04-25 18:54:26.304: w/system.err(2945): @ android.os.looper.loop(looper.java:137) 04-25 18:54:26.304: w/system.err(2945): @ android.app.activitythread.main(activitythread.java:4745) 04-25 18:54:26.304: w/system.err(2945): @ java.lang.reflect.method.invokenative(native method) 04-25 18:54:26.314: w/system.err(2945): @ java.lang.reflect.method.invoke(method.java:511) 04-25 18:54:26.314: w/system.err(2945): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:786) 04-25 18:54:26.334: w/system.err(2945): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553) 04-25 18:54:26.334: w/system.err(2945): @ dalvik.system.nativestart.main(native method)
the main aim of code load set of question , answers textview, not achieving.
try this... jsonobject js = new jsonobject(jsonstring); (int = 0; < js.length(); i++) { jsonarray data = js.getjsonarray("data"); (int j = 0; j < data.length(); j++) { jsonobject goaccess = data.getjsonobject(j); string question_json = goaccess.getstring("question"); string answer_json = goaccess.getstring("answer"); if(question_json ==null) question_json = ""; if(answer_json==null) answer_json = ""; log.i("question", question_json); log.i("answer", answer_json); question_answer_view.settext(question_json + answer_json); } }