java - how to parse a JSON string which is inside a array -
i have json of following format:
{ "result": { "question": "barack obama vs mitt romney?", "option": [ "barack obama", "mitt romney", "other" ], "percentage": [ 20, 40, 80 ] } }
and using following code parse giving null pointer exception @ option array.
jsonparser jparser = new jsonparser();
jsonobject json = jparser.getjsonobjectfromurl(url); log.e("json",json.tostring()); log.e("-------url-------", ""+url); string resultstr = json.getstring("result"); log.e("result string ",resultstr); jsonobject jsonobject2 = new jsonobject(resultstr); string question_string = jsonobject2.getstring("question"); log.e("question string ",question_string); string option_str = jsonobject2.getstring("option"); jsonarray optionarray = new jsonarray(option_str); log.d("option array", string.valueof(optionarray.length()));
you need json array way:
jsonarray optionarray = jsonobject2.getjsonarray("option"); log.d("option array", string.valueof(optionarray.length()));
check http://www.androidhive.info/2012/01/android-json-parsing-tutorial/
Comments
Post a Comment