android - JSON typeMismatch -
am trying parse info json getting error saying jsontype mismatch,
the code using parse info follow
arraylist = new arraylist<hashmap<string, string>>(); // retrieve json objects given url address jsonobject = jsonfunctions.getjsonfromurl(urlstring); seek { // locate array name in json jsonarray = jsonobject.getjsonarray("product"); (int = 0; < jsonarray.length(); i++) { hashmap<string, string> map = new hashmap<string, string>(); jsonobject = jsonarray.getjsonobject(i); // retrive json objects map.put("name", jsonobject.getstring("name")); map.put(appconstants.sub_category_href, jsonobject.getstring(appconstants.sub_category_href)) ; // set json objects array arraylist.add(map); } } grab (jsonexception e) { log.e("error", e.getmessage()); e.printstacktrace(); } the json array construction follows
{ "success": true, "product": { "id": "42", "name": "product name", "description": "<p>your product description here. product description here. product description here. product description here. product description here. product description here. product description here.</p> <p> your product description here. product description here. product description here. product description here. product description here. product description here. product description here. </p> ", "image": "http://test.com/image/cache/data/products/eyes-600x600.png", "image1": "http://test.com/image/cache/data/products/product2-600x600.png", "image2": "http://test.com/image/cache/data/products/lips-600x600.png", "image3": "http://test.com/image/cache/data/products/product6-600x600.png", "image4": null, "price": "100kd", "special": "70kd", "related products": [ { "product_id": "30", "thumb": "http://image/cache/data/products/product6-600x600.png", }, { "product_id": "35", "thumb": "http://image/cache/data/products/eyes-600x600.png", } ] } } the detailed logcat output giving
06-18 15:17:02.834: w/system.err(8183): atorg.json.json.typemismatch(json.java:100) 06-18 15:17:02.834: w/system.err(8183): @ org.json.jsonobject.getjsonarray(jsonobject.java:553) 06-18 15:17:02.834: w/system.err(8183): @ com.dairam.fragments.productdetailsfragment$downloadjson.doinbackground(productdetailsfragment.java:82) 06-18 15:17:02.834: w/system.err(8183): @ com.dairam.fragments.productdetailsfragment$downloadjson.doinbackground(productdetailsfragment.java:1) 06-18 15:17:02.834: w/system.err(8183): @ android.os.asynctask$2.call(asynctask.java:288) 06-18 15:17:02.844: w/system.err(8183): @ java.util.concurrent.futuretask.run(futuretask.java:237) 06-18 15:17:02.844: w/system.err(8183): @ android.os.asynctask$serialexecutor$1.run(asynctask.java:231) 06-18 15:17:02.854: w/system.err(8183): @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1112) 06-18 15:17:02.854: w/system.err(8183): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:587) 06-18 15:17:02.854: w/system.err(8183): @ java.lang.thread.run(thread.java:841)
as others have pointed out, line:
jsonarray = jsonobject.getjsonarray("product"); is trying retrieve array called product, doesn't exist. product object. can mitigate error, doing:
jsonobject = jsonobject.getjsonobject("product"); this assumes have changed type of variable. can access other parts of object jsonobject.get();
android json arrays
No comments:
Post a Comment