parse json data coming from webservice in java -


i developing android app getting web-service data as:

"[{\"id\":51,\"text\":\"!! sample project !!\"},{\"id\":58,\"text\":\"01 contracting test project\"},{\"id\":64,\"text\":\"1212\"},{\"id\":45,\"text\":\"chemical factory project\"}]" 

now want parse data in json used replaceall() function replace backslashes string this:

 string jsonformattedstring = line.replaceall("\\\\", ""); 

but think method isnot work because removes backslashes string creates problems recieved json node like:

"[{\"id\":9617,\"text\":\"1 1\/4\\\" pvc\/gi clamps\"}]" 

where string value text contains double quotes within string creates problem me. question best way parse json data in java.

my full json data returned webservice as:

"[{\"id\":51,\"text\":\"!! sample project !!\"},{\"id\":58,\"text\":\"01 contracting test project\"},{\"id\":64,\"text\":\"1212\"},{\"id\":45,\"text\":\"chemical factory project\"},{\"id\":53,\"text\":\"kanix city\"},{\"id\":54,\"text\":\"kanix dream city\"},{\"id\":59,\"text\":\"kanix dream city -- phase ii\"},{\"id\":62,\"text\":\"kanix dream city phase i\"},{\"id\":55,\"text\":\"kishor_test\"},{\"id\":63,\"text\":\"next generation housing\"},{\"id\":65,\"text\":\"nothing job\"},{\"id\":56,\"text\":\"pavan_test\"},{\"id\":46,\"text\":\"production units\"},{\"id\":1,\"text\":\"project-01(type 1)\"},{\"id\":3,\"text\":\"project-02(type 1)\"},{\"id\":5,\"text\":\"project-03(type 1)\"},{\"id\":6,\"text\":\"project-04(type 1)\"},{\"id\":7,\"text\":\"project-05(type 1)\"},{\"id\":8,\"text\":\"project-06(type 1)\"},{\"id\":2,\"text\":\"project-07(type 2)\"},{\"id\":4,\"text\":\"project-08(type 2)\"},{\"id\":9,\"text\":\"project-09(type 3)\"},{\"id\":10,\"text\":\"project-10(type 3)\"},{\"id\":11,\"text\":\"project-11(type 4)\"},{\"id\":57,\"text\":\"reviera classic\"},{\"id\":43,\"text\":\"road project\"},{\"id\":41,\"text\":\"sample project 1\"},{\"id\":42,\"text\":\"sample project 2\"},{\"id\":52,\"text\":\"shailesh test project@1000\"},{\"id\":61,\"text\":\"vishal paradise\"},{\"id\":60,\"text\":\"wtc\"}]" 

my full code this:

@override protected list<citem> doinbackground(string... params) {     try {     string line="";     string ur = "http://"+serverdetails.hostserver+"/appservices.svc/projects?keyword=" ;         lstitm=new arraylist<citem>() ;           // replace own wcf service path         url json = new url(ur);         urlconnection jc = json.openconnection();         bufferedreader reader = new bufferedreader(new inputstreamreader(jc.getinputstream()));           line = reader.readline();          log.d("line",line);            jsonarray array=new jsonarray(line);           itm=new citem( "-1", "select project" );         lstitm.add(itm);         for(int i=0; < array.length(); i++) {             jsonobject tmpjson=array.getjsonobject(i);               itm=new citem(tmpjson.getstring("id"),tmpjson.getstring("text"));             lstitm.add(itm);           }           return  lstitm ;           }     catch(exception e)     {         log.d("errror--->",e.getmessage());        }          return  lstitm ;  } 

@mubu9082 ..you dont need remove these backslashes... json string shown backslashes in log or debugger..

just parse usual

public void jsonparser() {     arraylist<> list=new arraylist<>(); //declare global     string responsestring="[{\"id\":51,\"text\":\"!! sample project !!\"},{\"id\":58,\"text\":\"01 contracting test project\"},{\"id\":64,\"text\":\"1212\"},{\"id\":45,\"text\":\"chemical factory project\"}]";     jsonarray array=new jsonarray(responsestring);     string id[]=new string[array.length()];     string text[]=new string[array.length()];     for(int i=0;i<array.length();i++)     {          jsonobject tmpjson=array.getjsonobject(i);          id[i]=tmpjson.getstring("id");          text[i]=tmpjson.getstring("text");          citem itm=new citem(tmpjson.getstring("id"),tmpjson.getstring("text")); lstitm.add(itm);          list.add(itm);     } } 

do response server

try {          // create httpclient         httpclient httpclient = new defaulthttpclient();          // make request given url ...use         httpresponse httpresponse = httpclient.execute(new httpget(url));          // receive response inputstream         httpentity entity = httpresponse.getentity();          string response= entityutils.tostring(entity);         //pass response jsonarray object         //save response , flush entity.         entity.consumecontent();      } catch (exception e) {         log.d("inputstream", e.getlocalizedmessage());     } 

pass response jsonarray object


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -