android - How do I pull the string name array from this json object? -
{"categories":[{"id":"1","name":"asdf"}]}
its json string. want value of name key .. how can in android ? pleass help
blockquote
here json parsing exception handling , null checks :
//get json string first response , convert json object jsonobject object; try { object = new jsonobject("yourjsonstring"); } catch (jsonexception e) { e.printstacktrace(); object = null; } jsonarray jsonarray; if (object != null) { object categoryobject = null; try { categoryobject = object.get("categories"); } catch (jsonexception e) { e.printstacktrace(); categoryobject = null; } if (categoryobject != null) { if (categoryobject instanceof jsonarray) { //if categoriew array having more 1 items jsonarray = (jsonarray) categoryobject; } else { //if categories array having single item jsonarray = new jsonarray(); jsonarray.put((jsonobject) categoryobject); } jsonobject categoryitemobj = null; (int = 0; < jsonarray.length(); i++) { try { categoryobject = jsonarray.getjsonobject(i); } catch (jsonexception e) { e.printstacktrace(); categoryitemobj = null; } if (categoryitemobj != null) { string id = ""; try { id = categoryitemobj.getstring("id"); } catch (jsonexception e) { e.printstacktrace(); id = ""; } string name; try { name = categoryitemobj.getstring("name"); } catch (jsonexception e) { e.printstacktrace(); name = ""; } } } } }
Comments
Post a Comment