java - Parse JSON which is within HTML in Tags in Android app -


this question has answer here:

my problem little different thought, ask previously:parse json cofigure android application have json coming server, when in browser source code ; looks

joson.config:

<html> <head></head> <body> <pre>  [      {         "sett": " ",         "glhdr": {             "sm": [ ],             "sclehpad": false,             "st": "sbsm"         },         "colrbg": [             23,             105,             184,             100         ],         "colrtb": [             0,             0,             0,             0         ],         "colrtr": [             255,             255,             255,             100         ],         "glftr": {             "icosz": "icoszn",             "sm": [ ],             "sclehpad": false,             "gvnr": 3,             "gvhit": false,             "gvnc": 3,             "st": "igsm"         },         "statbr": true     },     {         "sm": [             {                 "tbico": "b43-jeep.png",                 "t": "welcome!",                 "w": "http://google.com/start",                 "st": "w",                 "wtbl": "wtbln"             },             {                 "t": "content screen title",                 "f": "eltec%20spec%20sheet%20gim%20rd30w.pdf",                 "st": "f"             },             {                 "tbico": "109-chicken.png",                 "t": "sub menu",                 "sm": [                     {                         "t": "screen 1",                         "st": "f"                     },                     {                         "t": "screen 2",                         "w": "http://google.com",                         "st": "w",                         "wtbl": "wtblt"                     }                 ],                 "st": "sm"             },             {                 "st": "f"             }         ],         "st": "tbm"     }  ]  </pre> </body> </html> 

function scan json:

   public void doscanappconfigjson(){               jsonarray appconfig = null;              // function looping json object via parsejson class.             //creating json parser instance             jsonparser jparser = new jsonparser();              //getting json strings url             jsonobject jsonobject = jparser.getjsonfromurl(url);          try{             //getting array of settings             appconfig = jsonobject.getjsonarray(configconstants.table_view_sub_menu_config);             //loop throw objects under -sm[]             (int = 0; < appconfig.length(); i++){                  jsonobject sm = appconfig.getjsonobject(i);                  //now store each of json in local constant var.                  string tabtitle = sm.getstring(tag_title);                  string webaddress = sm.getstring(tag_web_address);                  string screentype = sm.getstring(tag_screen_type);                  string filename = sm.getstring(tag_filename);              }          }catch (jsonexception e){             e.printstacktrace();          }          } 

getjsonfromurl method:

public jsonobject getjsonfromurl(string url) {

    //global authentication link username , password.     authenticator.setdefault(new authenticator() {         protected passwordauthentication getpasswordauthentication(){          return new passwordauthentication("username", "password".tochararray());            }      });      // making http request     try {         // defaulthttpclient         defaulthttpclient httpclient = new defaulthttpclient();         httppost httppost = new httppost(url);          httpresponse httpresponse = httpclient.execute(httppost);         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, "iso-8859-1"), 8);         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 (jsonexception e) {         log.e("json parser", "error parsing data " + e.tostring());     }      // return json string     return jobj;  }  

and error im getting : http://cl.ly/image/0z1u2v30341g

now guess bcz of html doctype or else, can not find whats wrong.

q: how can parse json , store in local variables? (i don't have option of changing server side json)thanks in advance.

well can extract sub string starting opening '[' till closing ']' , parse json, make changes in getjsonfromurl() method:

public void getjsonfromurl() {      ...      ...       json = sb.tostring().substring(html.indexof("["), html.lastindexof("]") + 1);       ... } 

Comments