Parsing JSON in Android using AsyncHttpClient -
i need parse json android application, i’m getting error:
org.json.array cannot converted jsonobject
what want take json server , parse textviews had made i’m using asynchttpclient, here code.
asynchttpclient client1 = new asynchttpclient(); client1.get("http://mahmoudfa-001-site1.atempurl.com/appetizers.json",new texthttpresponsehandler() { @override public void onfailure ( int statuscode, header[] headers, string responsestring, throwable throwable){ } @override public void onsuccess ( int statuscode, header[] headers, string responsestring){ log.i("a1", responsestring); //testing if server responding. toast.maketext(getapplicationcontext(), responsestring, toast.length_long).show(); try { jsonobject job = new jsonobject(responsestring); jsonarray arr = new jsonarray(build); //string arrlen = integer.tostring(arr.length()); jsonobject na = arr.getjsonobject(0); jsonarray ingna = na.getjsonarray("unavailable"); string[] ingr = new string[ingna.length()]; (int k = 0; k < ingna.length(); k++) { jsonobject abc = ingna.getjsonobject(k); ingr[k] = abc.getstring("ingredient"); } (int = 1; < arr.length(); i++) { jsonobject food = null; food = arr.getjsonobject(i); string name = food.getstring("name"); string description = food.getstring("description"); string rating = food.getstring("rating"); string price = food.getstring("price"); string cooktime = food.getstring("cooktime"); jsonarray ingredients = food.getjsonarray("ingredients"); string[] ing = new string[ingredients.length()]; (int k = 0; k < ingredients.length(); k++) { jsonobject ingd = ingredients.getjsonobject(k); ing[k] = ingd.getstring("ingredient"); } (int l = 0; l < ing.length; l++) { (int m = 0; m < ingr.length; m++) { if (ing[l].matches(ingr[m])) ; } } } } catch (jsonexception e1) { // todo auto-generated catch block e1.printstacktrace(); } try { jsonobject job = new jsonobject(responsestring); jsonarray arr = new jsonarray(build); //string arrlen = integer.tostring(arr.length()); jsonobject na = arr.getjsonobject(0); jsonarray ingna = na.getjsonarray("unavailable"); string[] ingr = new string[ingna.length()]; (int k = 0; k < ingna.length(); k++) { jsonobject abc = ingna.getjsonobject(k); ingr[k] = abc.getstring("ingredient"); } (int = 1; < arr.length(); i++) { jsonobject food = null; food = arr.getjsonobject(i); string name1 = food.getstring("name"); if (name.equals(name1)) { imageurl[0] = imageurl[0] + i; nametext.settext("name : " + name); string description = food.getstring("description"); detailstext.settext("description : " + description); string rating = food.getstring("rating"); string price = food.getstring("price"); price1 = integer.parseint(price); pricetext.settext("price : rs. " + price); ratingtext.settext("rating : " + rating + " stars"); string cooktime = food.getstring("cooktime"); cooktimetext.settext("cooktime : " + cooktime); jsonarray ingredients = food.getjsonarray("ingredients"); string[] ing = new string[ingredients.length()]; (int k = 0; k < ingredients.length(); k++) { jsonobject ingd = ingredients.getjsonobject(k); ing[k] = ingd.getstring("ingredient"); } string ingre = "ingredients:"; (int k = 0; k < ing.length; k++) { if (k < (ing.length - 1)) ingre = ingre + " " + ing[k] + ","; else ingre = ingre + " " + ing[k]; } ingredientstext.settext(ingre); break; } } } catch (jsonexception e) { e.printstacktrace(); } } });
please need help.. json getting server:
[
{ "unavailable" : [ { "ingredient" : "prawn" },
{ "ingredient" : "paneerygf" }, { "ingredient" : "fish1" } ] }, { "name" : "paneer chilly", "description" : "fried paneer pieces in chilly gravy", "rating" : "4", "price" : "100", "cooktime" : "20 mins", "ingredients" : [ {"ingredient":"paneer"} ] }, { "name" : "prawn stuff papad", "description" : "papad stuffed prawns , spices", "rating" : "3", "price" : "150", "cooktime" : "25 mins", "ingredients" : [ {"ingredient":"prawn"} ] }, { "name" : "fish chilly", "description" : "fired fish fillets chilly gravy", "rating" : "3", "price" : "175", "cooktime" : "25 mins", "ingredients" : [ {"ingredient":"fish"} ] }
]
the response returned http://mahmoudfa-001-site1.atempurl.com/appetizers.json jsonarray, not jsonobject. should parse jsonarray jsonarray = new jsonarray(responsestring)
.
Comments
Post a Comment