Conversion between JSON strings and Java objects

Source: Internet
Author: User
Jsonlib classic article: http://json-lib.sourceforge.net/xref-test/net/sf/json/TestJSONObject.html

// Introduce the corresponding package

// Json-lib-2.2-jdk15.jar

Import net. SF. JSON. jsonarray;
Import net. SF. JSON. jsonobject;

1. // convert the Java object list to a JSON object array and convert it to a string

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

2. // convert a Java object to a JSON object and convert it to a string

  Jsonobject object = jsonobject. fromobject (invite );
  Log4jinit. ysulogger. debug (object. tostring ());

3. // convert the JSON string into a Java object Array

 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

 STR = "{\" lendperson \ ": \" Li Si \ ", \" lendcompany \ ": \" Company Limited \ ", \" checkperson \": \ "Li Si \",
  \ "Lenddate \": \ "2010-07-19t00: 00: 00 \", \ "lendcounts \": 4 ,\"
  Passports \ ": [{\" passportid \ ": \" d \ ", \" Name \ ": \" Li yuchuan \ ", \" passporttype \": \ "k \"},
  {\ "Passportid \": \ "k9051 \", \ "Name \": \ "Li Ping \", \ "passporttype \": \ "k \"},
  {\ "Passportid \": \ "k90517 \", \ "Name \": \ "Yuan hanmei \", \ "passporttype \": \ "k \"},
  {\ "Passportid \": \ "k905199 \", \ "Name \": \ "He Ming \", \ "passporttype \": \ "k \"}]} ";
   Jsonobject = jsonobject. fromobject (STR );
  Passportlendsentity passportlends = NULL;
  Try {
   // Obtain a JSON Array
   Jsonarray array = jsonobject. getjsonarray ("passports ");
   // Convert the 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 );
           } 
        }
   // Convert the passportlendsentity object class
   Passportlends = (passportlendsentity) jsonobject. tobean (jsonobject, passportlendsentity. Class );

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.