android - JSON String Parsing -


im trying parse json data sent php using android. here code. json output looks : "{documents:[{idnumber:'28044684',doctype:'national id'}],success:1}"

im getting error error parsing jsonobject value [null]; when directly assign json output variable json. problem??

string json = "";         list<namevaluepair> params = new arraylist<namevaluepair>();         params.add(new basicnamevaluepair("idnumber", parameter));         // making http request         try {              // check request method             if (method == "post") {                 // request method post                 // defaulthttpclient                 defaulthttpclient httpclient = new defaulthttpclient();                 httppost httppost = new httppost(url);                 httppost.setentity(new urlencodedformentity(params));                 httpresponse httpresponse = httpclient.execute(httppost);                 httpentity httpentity = httpresponse.getentity();                 = httpentity.getcontent();              } else if (method == "get") {                 // request method                 defaulthttpclient httpclient = new defaulthttpclient();                 string paramstring = urlencodedutils.format(params, "utf-8");                 url += "?" + paramstring;                 httpget httpget = new httpget(url);                 httpresponse httpresponse = httpclient.execute(httpget);                 httpentity httpentity = httpresponse.getentity();                 = httpentity.getcontent();             }          } catch (unsupportedencodingexception e) {             e.printstacktrace();         } catch (clientprotocolexception e) {             e.printstacktrace();         } catch (ioexception e) {             e.printstacktrace();         }          try {             bufferedreader reader = new bufferedreader(new inputstreamreader(is, "utf-8"), 300);             stringbuilder sb = new stringbuilder();             string line = null;             while ((line=reader.readline())!= null ) {                 sb.append(line + "\n");             }             is.close();             json = sb.tostring();         } catch (exception e) {             log.e("buffer error", "error converting result " + e.tostring());         }         // try parse string json object         try {             jobj = new jsonobject(json);         } catch (exception e) {             log.e("json parser", "error parsing data " + e.tostring());             try {                 jobj = new jsonobject(json.substring(json.indexof("{"),                         json.lastindexof("}") + 1));             } catch (exception e0) {                 log.e("json parser0",                         "error parsing data [" + e0.getmessage() + "] "                                 + json);                 log.e("json parser0", "error parsing data " + e0.tostring());                 try {                     jobj = new jsonobject(json.substring(1));                 } catch (exception e1) {                     log.e("json parser1",                             "error parsing data [" + e1.getmessage() + "] "                                     + json);                     log.e("json parser1",                             "error parsing data " + e1.tostring());                     try {                         jobj = new jsonobject(json.substring(2));                     } catch (exception e2) {                         log.e("json parser2",                                 "error parsing data [" + e2.getmessage()                                         + "] " + json);                         log.e("json parser2",                                 "error parsing data " + e2.tostring());                         try {                             jobj = new jsonobject(json.substring(3));                         } catch (exception e3) {                             log.e("json parser3", "error parsing data ["                                     + e3.getmessage() + "] " + json);                             log.e("json parser3", "error parsing data "                                     + e3.tostring());                         }                     }                 }             }         }         return null; 

your json invalid

{ documents: [     {         idnumber: '28044684',         doctype: 'nationalid'     } ], success: 1 } 

refer : http://json.org/example.html correct format

the correct format should be

{ "documents": [     {         "idnumber": "28044684",         "doctype": "nationalid"     } ], "success": 1 } 

validate here


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -