Packages that need to be introduced:
Jakarta Commons-lang 2.3
Jakarta commons-beanutils 1.7.0 Jakarta commons-collections 3.2 Jakarta
Commons-logging 1.1
ezmorph 1.0.4
Json-lib-2.4-jdk15.jar
Product.java
public class Product {
private String product;
private double price;
Private Object item;
Public Object GetItem () {return
item;
}
public void SetItem (Object) {
This.item = Item;
}
Public String getproduct () {return
product
}
public void Setproduct (String product) {
this.product = product;
}
Public double GetPrice () {return price
;
}
public void Setprice (double price) {
This.price = Price;
}
}
Jsonutil.java
public class Jsonutil {//{"Product": "Pencil", "Price": "Item": [{"id": 1, "name": "Chen"}]} private static Final String J
Sostr = "{\ product\": \ "pencil\", \ "price\": 12,\ "item\": [{\ "id\": 1,\ "name\": \ "Chen\"}]} ";
public static void Main (string[] args) throws IOException {//String System.out.println (String4json ("Key", "20"));
obj Product p = (product) getobject4jsonstring (JSOSTR, Product.class);
System.out.println (p.getproduct () + "," + P.getprice ());
Map map map = Getmap4json (JSOSTR); for (Iterator iterator = Map.entryset (). iterator (); Iterator.hasnext ();)
{Map.entry Entry = (map.entry) iterator.next ();
System.out.println (Entry.getkey () + "," + Entry.getvalue ());
}//Array object[] objects = Getstringarray4json (JSOSTR);
for (int i = 0; i < objects.length i++) {System.out.println (objects[i]); Convert string to JSON public static string String4json (string key, String value) throws Jsonexception {jsonobject O Bject = new Jsonobject ();
Object.put (key, value);
return object.tostring (); ///From a JSON object character format to get a Java object public static object Getobject4jsonstring (String jsonstring, Class pojocalss) {Object
Pojo
Jsonobject jsonobject = Jsonobject.fromobject (jsonstring);
Pojo = Jsonobject.tobean (Jsonobject, POJOCALSS);
return Pojo; ///Get a map public static map Getmap4json (String jsonstring) {jsonobject jsonobject = Jsonobject.fromobjec from a JSON expression
T (jsonstring);
Iterator Keyiter = Jsonobject.keys ();
String key;
Object value;
Map ValueMap = new HashMap ();
while (Keyiter.hasnext ()) {key = (String) keyiter.next ();
Value = Jsonobject.get (key);
Valuemap.put (key, value);
return VALUEMAP; ///Get a list of Java objects from the JSON object collection expression public static list Getlist4json (String jsonstring, Class pojoclass) {Jsonarray Jsonar
Ray = Jsonarray.fromobject (jsonstring);
Jsonobject Jsonobject;
Object Pojovalue;
List List = new ArrayList (); for (int i = 0; i < jsonarray.size (); i++) {JsonobjECT = Jsonarray.getjsonobject (i);
Pojovalue = Jsonobject.tobean (Jsonobject, Pojoclass);
List.add (Pojovalue);
} return list; ///Parse out Java string array public static object[] Getstringarray4json (string jsonstring) {Jsonobject jsonobj = Jsonob from JSON array
Ject.fromobject (jsonstring);
Jsonarray Jsonarr = Jsonobj.getjsonarray ("item");
Return (object[]) Jsonarr.toarray (); }