Java code Parsing string string (in JSON format)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.