Converting JSON strings to Java objects

Source: Internet
Author: User

First, the jar that Maven needs to rely on is introduced:

<dependency>    <groupId>net.sf.json-lib</groupId>    <  Artifactid>json-lib</artifactid>    <version>2.4</version >    <classifier>jdk15</classifier></dependency>
Classes that need to be imported:
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

jsonarrayarray = Jsonarray.fromobject (userlist); stringjsonstr = 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.//to convert 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\"}]} ";Jsonobjectjsonobject = Jsonobject.fromobject (str);Passportlendsentitypassportlends = null;try {Get a JSON arrayJsonarrayarray = Jsonobject.getjsonarray ("passports");Convert JSON arrays to list<passportforlendsentity> genericsList<passportforlendsentity>list = newarraylist<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 passportlendsentity entity classPassportlends= (passportlendsentity)Jsonobject.tobean(Jsonobject,passportlendsentity.class);

5. If you want to take only a few fields of the JSON string, there is no need to turn into a Java object, go directly to an object array, then convert the array to an object, then go to the value of a field
Jsonarray Listarray = Dataobject.getjsonarray ("list");  (i =0; i < listarray.size (); i++) {    tmp = Listarray.getjsonobject (i);    float longitude = tmp.getstring ("longitude");
}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Converting 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.