1. Convert the list of Java objects to an array of JSON objects and move to a string
Jsonarray array = jsonarray.fromobject (userlist);
String jsonstr = array.tostring ();
2. Converting Java objects to JSON objects and converting them into strings
Jsonobject Object = Jsonobject.fromobject (invite);
String str=object.tostring ());
3. Convert the 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. Convert the JSON string to a Java object
Jsonobject jsonobject = Jsonobject.fromobject (str);
Passportlendsentity passportlends = null;
try {
Get a JSON array
Jsonarray array = Jsonobject.getjsonarray ("passports");
Convert JSON arrays to list<passportforlendsentity> generics
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 passportlendsentity entity class
Passportlends = (passportlendsentity) Jsonobject.tobean (Jsonobject, Passportlendsentity.class);
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\"}]} ";
Four common scenarios in which Java object JSON data is converted to each other