1. Convert a list of Java objects to a JSON object array and move to a string
Copy Code code as follows:
Jsonarray array = jsonarray.fromobject (userlist);
String jsonstr = array.tostring ();
2. Converts a Java object into a JSON object and converts it to a string
Copy Code code as follows:
Jsonobject Object = Jsonobject.fromobject (invite);
String str=object.tostring ());
3. Convert a JSON string to a Java object array
Copy Code code as follows:
String personstr = Getrequest (). GetParameter ("persons");
Jsonarray json = Jsonarray.fromobject (PERSONSTR);
list<invoidperson> persons = (list<invoidperson>) jsonarray.tocollection (JSON, nvoidperson.class);
4. Convert a JSON string to a Java object
Copy Code code as follows:
Jsonobject jsonobject = Jsonobject.fromobject (str);
Passportlendsentity passportlends = null;
try {
Gets a JSON array
Jsonarray array = Jsonobject.getjsonarray ("passports");
Converts a JSON array to a list<passportforlendsentity> generic
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);
}
}
Converting passportlendsentity entity classes
Passportlends = (passportlendsentity) Jsonobject.tobean (Jsonobject, Passportlendsentity.class);
str = "{\" lendperson\ ": \" Dick \ "," lendcompany\ ": \" Limited ", \" checkperson\ ": \" Dick \ ",
\ "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\"}]} ";
Related JAR Packages:
PS: About JSON operation, here we recommend a few more practical JSON online tools for your reference to use:
Online JSON code inspection, inspection, landscaping, formatting tools:
Http://tools.jb51.net/code/json
JSON Online formatting tool:
Http://tools.jb51.net/code/jsonformat
Online Xml/json Mutual Conversion tool:
Http://tools.jb51.net/code/xmljson
JSON code online Format/beautify/compress/edit/Convert tools:
Http://tools.jb51.net/code/jsoncodeformat
Online JSON compression/escape tool:
Http://tools.jb51.net/code/json_yasuo_trans
C Language Style/html/css/json code formatting landscaping Tools:
Http://tools.jb51.net/code/ccode_html_css_json