1. Convert the list of Java objects to an array of JSON objects and move to a string
Copy CodeThe code is as follows:
Jsonarray array = jsonarray.fromobject (userlist);
String jsonstr = array.tostring ();
2. Converting Java objects to JSON objects and converting them into strings
Copy CodeThe code is as follows:
Jsonobject Object = Jsonobject.fromobject (invite);
String str=object.tostring ());
3. Convert the JSON string to an array of Java objects
Copy CodeThe code is as follows:
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
Copy CodeThe code is as follows:
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\"}]} ";
------------------------------------------------------code that you write yourself------------------------------------------------------------
Package com.springDemo.animal;
/**
* @ClassName: Cat
* @Description: The entity class used for testing
* @Author: Huangshijie
* @CreateDate: January 15, 2015 10:53:30
* @Modifior:
* @ModifyDate:
* @ModifyNotes:
*/
public class Cat {
private String name;
Private String sex;
private String color;
Public Cat () {}
Public Cat (String name,string sex,string color) {
THIS.name = name;
This.sex =sex;
This.color = color;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
Public String Getsex () {
return sex;
}
public void Setsex (String sex) {
This.sex = sex;
}
Public String GetColor () {
return color;
}
public void SetColor (String color) {
This.color = color;
}
}
Package com.springDemo.animal;
Import java.util.ArrayList;
Import Java.util.Iterator;
Import java.util.List;
Import Net.sf.json.JSONArray;
/**
* @ClassName: Entityfromjson
* @Description: ArrayList from JSON to entity class
* @Author: Huangshijie
* @CreateDate: January 15, 2015 10:52:53
* @Modifior:
* @ModifyDate:
* @ModifyNotes:
*/
public class Entityfromjson {
public static void Main (string[] args) {
Cat CAT1 = new Cat ("CAT1", "male", "black");
Cat Cat2 = new Cat ("Cat2", "female", "white");
Cat CAT3 = new Cat ("Cat3", "male", "grey");
Cat cat4 = new Cat ("CAT4", "female", "Black");
arraylist<cat> catlist = new arraylist<cat> ();
Catlist.add (CAT1);
Catlist.add (CAT2);
Catlist.add (CAT3);
Catlist.add (CAT4);
Jsonarray Jsonarray = Jsonarray.fromobject (catlist);
@SuppressWarnings ("Unchecked")
List<cat> catList2 = (list<cat>) jsonarray.tocollection (Jsonarray, Cat.class);
iterator<cat> Iterator = Catlist2.iterator ();
while (Iterator.hasnext ()) {
Cat Tmpcat = Iterator.next ();
System.out.print (Tmpcat.getname () + ",");
System.out.print (Tmpcat.getsex () + ",");
System.out.println (Tmpcat.getcolor ());
}
}
}
JSON type conversion "Go"