Net.sf.json relies on a lot of packages.
There are commons-collections, Commons-beanutils.jar,commons-httpclient.jar,commons-lang.jar,ezmorph-1.0.5.jar,morph-1.1.1.jar
- /**
- * Get a Java object from a JSON object character format, such as:
- * {"id": Idvalue, "name": Namevalue, "Abean": {"Abeanid": Abeanidvalue, ...}}
- * @param Object
- * @param clazz
- * @return
- */
- Public Static Object getdto (String jsonstring, Class clazz) {
- Jsonobject jsonobject = null;
- Try {
- Setdataformat2java ();
- Jsonobject = Jsonobject.fromobject (jsonstring);
- }catch(Exception e) {
- E.printstacktrace ();
- }
- return Jsonobject.tobean (Jsonobject, clazz);
- }
- /**
- * Get a Java object from a JSON object character format, where Beanslist is a collection of a type, such as:
- * {"id": Idvalue, "name": Namevalue, "Abean": {"Abeanid": Abeanidvalue, ...},
- * beanslist:[{}, {}, ...]}
- * @param jsonstring
- * @param clazz
- * @param the type of the Map collection property (key: Collection property name, Value: Collection property type Class) Eg: ("beanslist": Bean.class)
- * @return
- */
- Public Static Object getdto (String jsonstring, Class clazz, map map) {
- Jsonobject jsonobject = null;
- Try {
- Setdataformat2java ();
- Jsonobject = Jsonobject.fromobject (jsonstring);
- }catch(Exception e) {
- E.printstacktrace ();
- }
- return Jsonobject.tobean (Jsonobject, clazz, map);
- }
- /**
- * Get an array of Java objects from a JSON array, such as:
- * [{"id": Idvalue, "name": Namevalue}, {"id": Idvalue, "name": Namevalue}, ...]
- * @param Object
- * @param clazz
- * @return
- */
- Public Static Object[] Getdtoarray (String jsonstring, Class clazz) {
- Setdataformat2java ();
- Jsonarray array = jsonarray.fromobject (jsonstring);
- object[] obj = new object[array.size ()];
- for (int i = 0; i < array.size (); i++) {
- Jsonobject Jsonobject = Array.getjsonobject (i);
- Obj[i] = Jsonobject.tobean (Jsonobject, clazz);
- }
- return obj;
- }
- /**
- * Get an array of Java objects from a JSON array, such as:
- * [{"id": Idvalue, "name": Namevalue}, {"id": Idvalue, "name": Namevalue}, ...]
- * @param Object
- * @param clazz
- * @param map
- * @return
- */
- Public Static Object[] Getdtoarray (String jsonstring, Class clazz, map map) {
- Setdataformat2java ();
- Jsonarray array = jsonarray.fromobject (jsonstring);
- object[] obj = new object[array.size ()];
- for (int i = 0; i < array.size (); i++) {
- Jsonobject Jsonobject = Array.getjsonobject (i);
- Obj[i] = Jsonobject.tobean (Jsonobject, clazz, map);
- }
- return obj;
- }
- /**
- * Get a collection of Java objects from a JSON array
- * @param Object
- * @param clazz
- * @return
- */
- Public Static List getdtolist (String jsonstring, Class clazz) {
- Setdataformat2java ();
- Jsonarray array = jsonarray.fromobject (jsonstring);
- List List = new ArrayList ();
- for (Iterator iter = Array.iterator (); Iter.hasnext ();) {
- Jsonobject Jsonobject = (jsonobject) iter.next ();
- List.add (Jsonobject.tobean (Jsonobject, clazz));
- }
- return list;
- }
- /**
- * Get a collection of Java objects from a JSON array with the object containing the collection properties
- * @param Object
- * @param clazz
- * @param the type of the Map collection property
- * @return
- */
- Public Static List getdtolist (String jsonstring, Class clazz, map map) {
- Setdataformat2java ();
- Jsonarray array = jsonarray.fromobject (jsonstring);
- List List = new ArrayList ();
- for (Iterator iter = Array.iterator (); Iter.hasnext ();) {
- Jsonobject Jsonobject = (jsonobject) iter.next ();
- List.add (Jsonobject.tobean (Jsonobject, Clazz, map));
- }
- return list;
- }
- /**
- * Get a map from a JSON hash expression that supports nested functions
- * Shape: {"id": "Johncon", "name": "Xiao Qiang"}
- * Note that the Commons-collections version must contain ORG.APACHE.COMMONS.COLLECTIONS.MAP.MULTIKEYMAP
- * @param Object
- * @return
- */
- Public Static Map Getmapfromjson (String jsonstring) {
- Setdataformat2java ();
- Jsonobject jsonobject = Jsonobject.fromobject (jsonstring);
- Map map = new HashMap ();
- for (Iterator iter = Jsonobject.keys (); Iter.hasnext ();) {
- String key = (string) iter.next ();
- Map.put (Key, Jsonobject.get (key));
- }
- return map;
- }
- /**
- * Get the appropriate Java array from the JSON array
- * JSON-shaped like: ["123", "456"]
- * @param jsonstring
- * @return
- */
- Public Static Object[] Getobjectarrayfromjson (String jsonstring) {
- Jsonarray Jsonarray = Jsonarray.fromobject (jsonstring);
- return Jsonarray.toarray ();
- }
- /**
- * Convert data Objects to JSON strings
- * Dto Object shape: {"id": Idvalue, "name": Namevalue, ...}
- * Array Object shape such as: [{}, {}, {}, ...]
- * Map Object shape: {key1: {"id": Idvalue, "name": Namevalue, ...}, Key2: {}, ...}
- * @param Object
- * @return
- */
- Public Static String getjsonstring (Object object) throws exception{
- String jsonstring = null;
- Date Value processor
- Jsonconfig jsonconfig = new jsonconfig ();
- Jsonconfig.registerjsonvalueprocessor (java.util.Date. Class, new jsondatevalueprocessor ());
- if (Object! = null) {
- if (Object instanceof Collection | | object instanceof object[]) {
- jsonstring = Jsonarray.fromobject (object, Jsonconfig). toString ();
- }Else{
- jsonstring = Jsonobject.fromobject (object, Jsonconfig). toString ();
- }
- }
- return jsonstring = = null ? "{}": jsonstring;
- }
JSON is parsed using Net.sf.json in Java