java - Unable to convert null String into JSON object -
this question has answer here:
this code work fine me when take output response in form of string
code is:
try { string id; string uname; string arg = "{\"id\":\"ets7qkt1luugsj828jugs8vuq5\",\"module_name\":\"users\",\"name_value_list\":{\"user_id\":{\"name\":\"user_id\",\"value\":\"1\"},\"user_name\":{\"name\":\"user_name\",\"value\":\"dbmadmin\"},\"user_language\":{\"name\":\"user_language\",\"value\":\"en_us\"},\"user_currency_id\":{\"name\":\"user_currency_id\",\"value\":\"-99\"},\"user_is_admin\":{\"name\":\"user_is_admin\",\"value\":true},\"user_default_team_id\":{\"name\":\"user_default_team_id\",\"value\":null},\"user_default_dateformat\":{\"name\":\"user_default_dateformat\",\"value\":\"d-m-y\"},\"user_default_timeformat\":{\"name\":\"user_default_timeformat\",\"value\":\"h:ia\"},\"user_number_seperator\":{\"name\":\"user_number_seperator\",\"value\":\",\"},\"user_decimal_seperator\":{\"name\":\"user_decimal_seperator\",\"value\":\".\"},\"mobile_max_list_entries\":{\"name\":\"mobile_max_list_entries\",\"value\":null},\"mobile_max_subpanel_entries\":{\"name\":\"mobile_max_subpanel_entries\",\"value\":null},\"user_currency_name\":{\"name\":\"user_currency_name\",\"value\":\"indian rupee\"}}}"; jsonobject obj = new jsonobject(arg); id = obj.get("id").tostring(); log.e("temp", "id "+id); jsonobject objuserlist = obj.getjsonobject("name_value_list"); jsonobject objusername = objuserlist.getjsonobject("user_name"); uname = objusername.getstring("value"); log.e("temp", "uname = "+uname); tvtest.settext(uname); log.d("temp", "json "+objuserlist.tostring()); } catch (jsonexception e) { log.e("log_tag", "error parsing data "+e.tostring()); } } }
when retrieve code server gives me error java.lang.string.cannot converted jsonobject
. code is:
try { bufferedreader reader = new bufferedreader(new inputstreamreader(is,"iso-8859-1"),8); stringbuilder sb = new stringbuilder(); string line = null; while ((line = reader.readline()) != null) { sb.append(line+"\n"); // toast.maketext(getapplicationcontext(), "input reading pass", toast.length_short).show(); } is.close(); result=sb.tostring(); log.i("sbvalue",result); } catch(exception e) { log.e("log_tag", "error converting result "+e.tostring()); toast.maketext(getapplicationcontext(), " input reading fail", toast.length_short).show(); } try{ jsonobject obj = new jsonobject(result); string id = obj.get("id").tostring(); log.d("temp", "id "+id); jsonobject objuserlist = obj.getjsonobject("name_value_list"); jsonobject objusername = objuserlist.getjsonobject("user_name"); string uname = objusername.getstring("value"); log.d("temp", "uname = "+uname);
the error get:
error parsing data org.json.jsonexception: value {"id":"e8nod37hu0dtuu78oue2pg52u3","module_name":"users","name_value_list":{"user_id": {"name":"user_id","value":"1"},"user_name": {"name":"user_name","value":"dbmadmin"},"user_language": {"name":"user_language","value":"en_us"},"user_currency_id": {"name":"user_currency_id","value":"-99"},"user_is_admin": {"name":"user_is_admin","value":true},"user_default_team_id": {"name":"user_default_team_id","value":null},"user_default_dateformat": {"name":"user_default_dateformat","value":"d-m-y"},"user_default_timeformat": {"name":"user_default_timeformat","value":"h:ia"},"user_number_seperator": {"name":"user_number_seperator","value":","},"user_decimal_seperator": {"name":"user_decimal_seperator","value":"."},"mobile_max_list_entries": {"name":"mobile_max_list_entries","value":null},"mobile_max_subpanel_entries": {"name":"mobile_max_subpanel_entries","value":null},"user_currency_name": {"name":"user_currency_name","value":"indian rupee"}}} of type java.lang.string cannot converted jsonobject
you have null values. jsonobject can't accept null values.
Comments
Post a Comment