Four common scenarios for converting Java object JSON data to each other _java

Source: Internet
Author: User

1. Convert a list of Java objects to a JSON object array and move to a string

Copy Code code as follows:

Jsonarray array = jsonarray.fromobject (userlist);
String jsonstr = array.tostring ();

2. Converts a Java object into a JSON object and converts it to a string
Copy Code code as follows:

Jsonobject Object = Jsonobject.fromobject (invite);
String str=object.tostring ());

3. Convert a JSON string to a Java object array
Copy Code code as follows:

String personstr = Getrequest (). GetParameter ("persons");
Jsonarray json = Jsonarray.fromobject (PERSONSTR);
list<invoidperson> persons = (list<invoidperson>) jsonarray.tocollection (JSON, nvoidperson.class);

4. Convert a JSON string to a Java object
Copy Code code as follows:

Jsonobject jsonobject = Jsonobject.fromobject (str);
Passportlendsentity passportlends = null;
try {
Gets a JSON array
Jsonarray array = Jsonobject.getjsonarray ("passports");
Converts a JSON array to a list<passportforlendsentity> generic
list<passportforlendsentity> list = new arraylist<passportforlendsentity> ();
for (int i = 0; i < array.size (); i++) {
Jsonobject object = (jsonobject) array.get (i);
Passportforlendsentity Passport = (passportforlendsentity) Jsonobject.tobean (object,
Passportforlendsentity.class);
if (passport!= null) {
List.add (Passport);
}
}
Converting passportlendsentity entity classes
Passportlends = (passportlendsentity) Jsonobject.tobean (Jsonobject, Passportlendsentity.class);

str = "{\" lendperson\ ": \" Dick \ "," lendcompany\ ": \" Limited ", \" checkperson\ ": \" Dick \ ",

\ "lenddate\": \ "2010-07-19t00:00:00\", \ "lendcounts\": 4,\ "
Passports\ ": [{\ passportid\]: \" d\ "," name\ ": \" Li Shuichang \ "," passporttype\ ": \" K\ "},
{\ "passportid\": \ "k9051\", \ "name\": \ "Li ping \", "passporttype\": \ "K\"},
{\ "passportid\": \ "k90517\", "name\": \ "Yuan Han mei \", "passporttype\": \ "K\"},
{\ "passportid\": \ "k905199\", \ "name\": \ "He Ming \", \ "passporttype\": \ "K\"}]} ";

Related JAR Packages:

PS: About JSON operation, here we recommend a few more practical JSON online tools for your reference to use:

Online JSON code inspection, inspection, landscaping, formatting tools:
Http://tools.jb51.net/code/json

JSON Online formatting tool:
Http://tools.jb51.net/code/jsonformat

Online Xml/json Mutual Conversion tool:
Http://tools.jb51.net/code/xmljson

JSON code online Format/beautify/compress/edit/Convert tools:
Http://tools.jb51.net/code/jsoncodeformat

Online JSON compression/escape tool:

Http://tools.jb51.net/code/json_yasuo_trans

C Language Style/html/css/json code formatting landscaping Tools:
Http://tools.jb51.net/code/ccode_html_css_json

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.