| 1/**2 * @author ZWQ3 * **/4 public class Jsontest {5 public static void main (string[] args) {6 //***********************************************************************7 //JSON basic Test 8 {9 //Create a new JSON array Jsonarray Jsonarray = new Jsonarray (); //New A JSON object Jsonobject json1 = new Jsonobject (); &N Bsp Json1.put ("id", "1"), + json1.put ("name", "Zhang San"); & nbsp Json1.put ("Password", "123456"), //Add a JSON object to the JSON array nbsp Jsonarray.add (Json1), 1819 Jsonobject json2 = new JSO Nobject (); 20 json2.put ("id", "2"), + json2.put ("NA Me "," John Doe "), json2.put (" Password "," 654321 "); &NB Sp Jsonarray.add (Json2), SYSTEM.OUT.PRINTLN ("JSON array basic test:" + Jsonarray); 2526 //results: + //[{"id": "1", "name": "Zhang San", "Password": "123456"},{"id": "2", "Name": "John Doe", "Password": "654321"}]28 }2930 //***********************************************************************31 //Convert object to JSON string + {+ Book book = new book (); 3 4 Book.setname ("Java Starter Tutorial"), Book.set Price (52.3), &NBSP; //Convert Java objects to JSON objects PNs Jsonobject jsonobject = Jsonobject.fromobject ( Book), SYSTEM.OUT.PRINTLN ("from Object to Jsonobject:" + jsonobject.tostring ()); 3940 //Results: $ //from object to jsonobject:{"NA Me ": Java Getting Started Tutorial", "Price": 52.3}42 }4344 //************************* //Convert a collection of objects into a JSON string-- & nbsp { list<book> List = new arraylist<book> (); Book Book1 = new book (), $ Book1.setname ("Advanced Math I"), Book1.setprice (34.1), a Book Book2 = new Boo K (); Book2.setname ("linear algebra"), Book2.setprice &N (12.7); Bsp List.add (Book1), List.add (BOOK2), + //Convert list collection to JSON array Jsonarray jsonarray= JSONARRAY.F Romobject (list), SYSTEM.OUT.PRINTLN ("from object collection to Jsonarray:" + Jsonarray.tostring ()) 5960 //results: + //From Object collection to jsonarray:[{"name": "Advanced Math I", "Price": 34.1},{"name": "Linear Algebra", "Price": 12.7}]62 }6364 //***********************************************************************65 //Convert JSON string to Java object-- {strin G jsonstring = "{name: ' data structure ', price:52.3}"; nbsp Jsonobject jsonobject = Jsonobject.fromobject (jsonstring); &NBSP ; //Convert a JSON object to Java object- Book book = (book) Jsonobject.tobean (Jsonobject, book . Class), SYSTEM.OUT.PRINTLN (book.tostring ()), 7273 /results:}7677 //Data structure 52.375 &N Bsp //***********************************************************************78 //Convert a JSON string to a Java object array {+- //(2). Array of beans String jsonsstring = "[{name: ' Database Basics ', Price:52.3},{name: ' Oracle 11g essence ' , price:42.3}] "; Jsonarray Jsonarray = Jsonarray.fromobject (jsonsstring); &n Bsp &NBSp Convert JSON array to Java object array book[] books = (book[]) Jsonarray.toarray (Jsonarray, book. Class), for (book B:books) {$ &NB Sp System.out.println (b.tostring ()); &NB }8889 Sp //Results: //Database Basics 52.391 //oracle 11g Essence 42.392 }93 }94} |