How to parse unnamed JSON Array in Android app -
i have json array sent sql server via php in following format finding difficult parse without encountering errors.
[ { "placename": "place1", "latitude": "50", "longitude": "-0.5", "question": "place1 existed when?", "answer1": "1800", "answer2": "1900", "answer3": "1950", "answer4": "2000", "correctanswer": "1900" }, { "placename": "place2", "latitude": "51", "longitude": "-0.5", "question": "place2 existed when?", "answer1": "800", "answer2": "1000", "answer3": "1200", "answer4": "1400", "correctanswer": "800" }, { "placename": "place3", "latitude": "52", "longitude": "-1", "question": "place 3 established when?", "answer1": "2001", "answer2": "2005", "answer3": "2007", "answer4": "2009", "correctanswer": "2009" } ]
i have verified json @ jsonlint , comes valid. have used log code print out json in eclipse app debugger after http client has processed , works fine (it shows json above know has downloaded correctly).
i'm trying fit json parser following activity attempts far have either contained many errors run or have returned no results because of json parsing errors.
here code of main activity. code activity adapted newthinktank.com (android development 15) , i'm trying tweak needs structure of json used in example different mine.
i hoping suggest code, or give me pointers, how go parsing json array properly. new android programming steep task figure out on own.
thanks time.
public class mainactivity extends activity { // json rest service pull static string dlquiz = "http://exampleserver.php"; // hold values pull json static string placename = ""; static string latitude = ""; static string longitude = ""; static string question = ""; static string answer1 = ""; static string answer2 = ""; static string answer3 = ""; static string answer4 = ""; static string correctanswer = ""; @override public void oncreate(bundle savedinstancestate) { // saved data super.oncreate(savedinstancestate); // point name layout xml file used setcontentview(r.layout.main); // call doinbackground() in myasynctask executed new myasynctask().execute(); } // use asynctask if need perform background tasks, need // change components on gui. put background operations in // doinbackground. put gui manipulation code in onpostexecute private class myasynctask extends asynctask<string, string, string> { protected string doinbackground(string... arg0) { // http client supports streaming uploads , downloads defaulthttpclient httpclient = new defaulthttpclient(new basichttpparams()); // define want use post method grab data // provided url httppost httppost = new httppost(dlquiz); // web service used defined httppost.setheader("content-type", "application/json"); // used read data url inputstream inputstream = null; // hold whole data gathered url string result = null; try { // response if web service httpresponse response = httpclient.execute(httppost); // content requested url along headers, etc. httpentity entity = response.getentity(); // main content url inputstream = entity.getcontent(); // json utf-8 default // bufferedreader reads data inputstream until buffer full bufferedreader reader = new bufferedreader(new inputstreamreader(inputstream, "utf-8"), 8); // store data stringbuilder thestringbuilder = new stringbuilder(); string line = null; // read in data buffer untilnothing left while ((line = reader.readline()) != null) { // add data buffer stringbuilder thestringbuilder.append(line + "\n"); } // store complete data in result result = thestringbuilder.tostring(); } catch (exception e) { e.printstacktrace(); } { // close inputstream when you're done try{if(inputstream != null)inputstream.close();} catch(exception e){} } //this allowed me verify json download in debugger log.v("jsonparser result ", result); // json parsing needs happen here... return result; } protected void onpostexecute(string result){ // gain access can change textviews textview line1 = (textview)findviewbyid(r.id.line1); textview line2 = (textview)findviewbyid(r.id.line2); textview line3 = (textview)findviewbyid(r.id.line3); // change values textviews line1.settext("place name: " + placename); line2.settext("question: " + question); line3.settext("correct answer: " + correctanswer); } }
}
check answer out: how parse json in android
you'll using:
jsonarray array = new jsonarray(result);
from there, you'll loop through , each jsonobject:
for(int = 0; < array.length(); i++) { jsonobject obj = array.getjsonobject(i); //now, whatever value need object: placename = obj.getstring("placename"); //or if on mainui thread can set textview here: yourtextview.settext(obj.getstring("placename")); }
good luck!