Java Parsing string string (JSON format)
Jar Package Required: Json-lib-2.4-jdk15.jar
One
String str = "{\" name\ ": \" zhangsan\ ", \" password\ ": \" zhangsan123\ ", \" email\ ": \" [Email protected]\ "}";
At this time with Jsonobject:
1 String str = "{\" name\ ": \" zhangsan\ ", \" password\ ": \" zhangsan123\ ", \" email\ ": \" [Email protected]\ "}"; 2 jsonobject json = jsonobject.fromobject (str); 3 System.out.println (json.getstring ("name"));
Two
String jsonstring = "[{\" name\ ": \" zhangsan\ ", \" password\ ": \" zhangsan123\ ", \" email\ ": \" [Email Protected]\ "}"
+ " , {\ "name\": \ "lisi\", \ "password\": \ "lisi123\", \ "email\": \ "[Email protected]\"}] ";
At this time with Jsonarray:
1String jsonstring = "[{\" name\ ": \" zhangsan\ ", \" password\ ": \" zhangsan123\ ", \" email\ ": \" [Email protected]\ "}"2+ ", {\" name\ ": \" lisi\ ", \" password\ ": \" lisi123\ ", \" email\ ": \" [Email protected]\ "}]";3Jsonarray Jsonarray =Jsonarray.fromobject (jsonstring);4 for(inti = 0; i < jsonarray.size (); i++){5System.out.println ("name =" +jsonarray.getjsonobject (i). getString ("name"));6SYSTEM.OUT.PRINTLN ("Password =" +jsonarray.getjsonobject (i). getString ("Password"));7System.out.println ("email =" +jsonarray.getjsonobject (i). getString ("email")));8}
The results of the two outputs are:
Zhangsan========================================== = = 10371443= == [email protected]
Java code Parsing string string (in JSON format)