hello facing problem here. have json string , parse data this:
@override protected list<questionslist> doinbackground(string... params) { namevaluepairs = new arraylist<>(); try { url = new url(params[0]); httpurlconnection = (httpurlconnection) url.openconnection(); httpurlconnection.setreadtimeout(10000); httpurlconnection.setconnecttimeout(15000); httpurlconnection.setrequestmethod("post"); httpurlconnection.setdoinput(true); httpurlconnection.setdooutput(true); setupdatatodb(); outputstream = httpurlconnection.getoutputstream(); bufferedwriter = new bufferedwriter(new outputstreamwriter(outputstream)); bufferedwriter.write(stringgenerator.queryresults(namevaluepairs)); bufferedwriter.flush(); bufferedwriter.close(); outputstream.close(); httpurlconnection.connect(); inputstream = new bufferedinputstream(httpurlconnection.getinputstream()); jsonresult = stringgenerator.inputstreamtostring(inputstream, questionsactivity.this); jsonresponse = new jsonobject(jsonresult.tostring()); log.e("response: ", jsonresult.tostring()); checkdisplaylanguage(langtext); questionslists = new arraylist<>(); (int = 0; < jsonmainnode.length(); i++) { jsonchildnode = jsonmainnode.getjsonobject(i); questionname = jsonchildnode.optstring(constants.question_name_json_name); log.e("question name: ", questionname); jsonarray = jsonchildnode.optjsonarray(constants.questions_answers_array); question_answers = new arraylist<>(); question_iscorrect = new arraylist<>(); (int j = 0; j < jsonarray.length(); j++) { jsonsecondchildnode = jsonarray.getjsonobject(j); answer1 = jsonsecondchildnode.optstring("answer1"); log.e("answer1", answer1); answer2 = jsonsecondchildnode.optstring("answer2"); log.e("answer2", answer2); answer3 = jsonsecondchildnode.optstring("answer3"); log.e("answer3", answer3); iscorrect1 = jsonsecondchildnode.optstring("iscorrect1"); iscorrect2 = jsonsecondchildnode.optstring("iscorrect2"); iscorrect3 = jsonsecondchildnode.optstring("iscorrect3"); question_answers.add(answer1); question_answers.add(answer2); question_answers.add(answer3); question_iscorrect.add(iscorrect1); question_iscorrect.add(iscorrect2); question_iscorrect.add(iscorrect3); log.e("answers in loop", question_answers.tostring()); questionslists.add(new questionslist(questionname, question_answers, question_iscorrect)); } } } catch (ioexception | jsonexception e) { e.printstacktrace(); } return questionslists; }
and output this:
e/question name:: find journal articles e/answers in for loop: [in librarys catalog , , ] e/answers in for loop: [in librarys catalog , , , , in alphabetical list of healink , ] e/answers in for loop: [in librarys catalog , , , , in alphabetical list of healink , , , , databases available in library's site] e/question name:: information provide magazine e/answers in for loop: [published research experiments current information, , ] e/answers in for loop: [published research experiments current information, , , , lists information people, addresses, organizations, ] e/answers in for loop: [published research experiments current information, , , , lists information people, addresses, organizations, , , , legislation, competitions] e/question name:: issn (international standard serial number) e/answers in for loop: [is number used registration of periodical publications, , ] e/answers in for loop: [is number used registration of periodical publications, , , , international unique number used registration of printed books, ] e/answers in for loop: [is number used registration of periodical publications, , , , international unique number used registration of printed books, , , , international unique number used recording of publications mixed forms]
i want displayed this:
e/answers in for loop: [is number used registration of periodical publications, international unique number used registration of printed books, international unique number used recording of publications mixed forms]
how possible?
you parsing data in wrong way, second loop redundunt , it's reason data being displayed wrong.
basically on every iteration pulling answer on i'th place, since there no data 2nd , 3rd place on first iteration 1st , 2nd place on array (hence second , third since array 0 based) empty on first time, on second iteration 3rd , 4th (hence fourth , fifth) empty , 6th had data , on...
in case 3 answers can remove loop , address 0, 1, 2 locations on array answers - job.
in case want more generic way switch code below -
(int j = 0; j < jsonarray.length(); j++) { jsonsecondchildnode = jsonarray.getjsonobject(j); answer = jsonsecondchildnode.optstring("answer" + (j+1)); log.e("answer", answer); iscorrect = jsonsecondchildnode.optstring("iscorrect" + (j+1)); question_answers.add(answer); question_iscorrect.add(iscorrect); log.e("answers in loop", question_answers.tostring()); }
also line:
questionslists.add(new questionslist(questionname, question_answers, question_iscorrect));
should outside of both loops, since there gathering data done...
Comments
Post a Comment