PS: Look at this blog before you first introduce the toolkit Json-lib-2.2.2-jdk15.jar
Resources Link: Baidu Cloud: Link: https://pan.baidu.com/s/1o9k7PSu Password: 00LJ
One, from object to string
To construct a jsonobject or Jsonarray object with object, then call its ToString () method to
(1) Example one
Book book=new book (); Book.setname ("Java"); Book.setprice (52.3f); Jsonobject Object=jsonobject.fromobject (book); System.out.println (Object.ToString ());
View Code
(2) Example two
Book book=new book (); Book.setname ("Java"); Book.setprice (52.3f) ; Book Book2=new book (), Book2.setname ("C"), Book2.setprice (42.3f); List List=new ArrayList (); List.add (book); List.add (BOOK2); Jsonarray Arry=jsonarray.fromobject (list); System.out.println (Arry.tostring ()); // The results are as follows: [{"Name": "Java", "Price": 52.3},{"name": "C", "Price": 42.3}]
View Code
Second, from string to object
To construct a jsonobject or Jsonarray object with a string object first
(1) Example one
String json= "{name: ' Java ', price:52.3}"; Jsonobject Object=jsonobject.fromobject (JSON); System.out.println (Object.get ("name") + "" "+object.get (" price ");
View Code
(2) Example two
String json= "[{name: ' Java ', price:52.3},{name: ' C ', price:42.3}]"; Jsonarray array=jsonarray.fromobject (JSON); for (int i=0;i<array.size (); i++) { map o=(map) array.get (i); System.out.println (O.get ("name") + "" "+o.get (" price ");}
View Code
Three, from string to bean
(1) Single Bean object
String json= "{name: ' Java ', price:52.3}"; Jsonobject Object=jsonobject.fromobject (JSON); Product Product= (product) Jsonobject.tobean (object,product. Class); System.out.println (Product.getname ()+ "" +product.getprice ());
View Code
(2) Array of beans
String json= "[{name: ' Java ', price:52.3},{name: ' C ', price:42.3}]"; Jsonarray array=jsonarray.fromobject (JSON); Product[]products = (product[]) Jsonarray.toarray (array,product. Class); for (Product p:products) { System.out.println (p.getname ()+ "" +P.getprice ());}
View Code
Iii. customizing classes that encapsulate JSON operations
PackageCom.util;Importjava.util.List;ImportJava.util.Map;ImportNet.sf.json.JSONArray;ImportNet.sf.json.JSONObject; Public classJsonhelper {//converting from a normal bean to a string Public StaticString Getjson (Object o) {jsonobject Jo=Jsonobject.fromobject (o); returnjo.tostring (); } //converting from a list of Java to a string Public StaticString Getjson (List list) {Jsonarray ja=jsonarray.fromobject (list); returnja.tostring (); } //converting from a Java object array to a string Public StaticString Getjson (object[] arry) {jsonarray ja=Jsonarray.fromobject (Arry); returnja.tostring (); } //Convert a string from JSON format to a map object Public StaticMap GetObject (String s) {returnJsonobject.fromobject (s); } //convert from JSON-formatted string to list array Public StaticList GetArray (String s) {returnJsonarray.fromobject (s); } //converting from a JSON-formatted string to a bean Public StaticObject getObject (String s,class cls) {jsonobject Jo=Jsonobject.fromobject (s); returnJsonobject.tobean (Jo, CLS); } //converting from JSON-formatted strings to arrays of objects of a class Public StaticObject GetArray (String s,class cls) {jsonarray ja=Jsonarray.fromobject (s); returnjsonarray.toarray (JA, CLS); }}View Code
Json-lib using--jsonobject and Jsonarray