Conversion of JSON strings to Java objects

Source: Internet
Author: User

Jsonlib Classic article: http://json-lib.sourceforge.net/xref-test/net/sf/json/TestJSONObject.html

Introduce the appropriate package

Json-lib-2.2-jdk15.jar

Import Net.sf.json.JSONArray; Import Net.sf.json.JSONObject;

1.//Convert the list of Java objects to a JSON object array and turn to a string

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

2.//Converts a Java object into a JSON object and converts it into a string

Jsonobject object = jsonobject.fromobject(invite); Log4jInit.ysulogger.debug (Object.ToString ());

3.//converting a JSON string to an array of Java objects

  String personstr = Getrequest (). GetParameter ("Persons");   jsonarray json = Jsonarray.fromobject ( PERSONSTR);   List<InvoidPerson> persons = (list<invoidperson>) Jsonarray.tocollection (JSON, nvoidperson.class); 4.//converts a JSON string to a Java object

  str = "{\" lendperson\ ": \" John Doe \ ", \" lendcompany\ ": \" limited \ ", \" checkperson\ ": \" John Doe \ ",   \" 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\ "}]}";    jsonobject jsonobject = Jsonobject.fromobject (str);   passportlendsentity passportlends = null;   try {   //Gets a JSON array    jsonarray array = Jsonobject.getjsonarray (" Passports ");    //converting JSON arrays to list<passportforlendsentity> generics    List< passportforlendsentity> list = new arraylist<passportforlendsentity> ();    for (int i = 0; i < array.size (); i++) {    &NBSP;&NBsp;          Jsonobject object = (jsonobject) array.get (i);                passportforlendsentity Passport = ( passportforlendsentity)Jsonobject.tobean(object, Passportforlendsentity.class);             if (passport! = null) {list.add (passport); }}//convert passportlendsentity entity class Passportlends = (passportlendsentity)Jsonobject.tobean(Jsonobject, Passportlendsentity.class);

Conversion of JSON strings to Java objects

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.