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
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 array
Jsonarrayarray = Jsonobject.getjsonarray ("passports");
Convert JSON arrays to list<passportforlendsentity> generics
List<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 class
Passportlends= (passportlendsentity)Jsonobject.tobean(Jsonobject,passportlendsentity.class);
Conversion of JSON strings to Java objects