// Data is the object data in JSON format.
Baseinfopojo jsonbaseinfopojo = (baseinfopojo) jsonutil. getdto (data,
Baseinfopojo. Class );
Public class jsonutil {
/**
* Get a Java object from the character format of a JSON object, such:
* @ Param jsonstring JSON character
* @ Param clazz converts the class of the Java object
* @ Return returns a Java object
*/
Public static object getdto (string jsonstring, class clazz ){
Jsonobject = NULL;
Setdataformat2java ();
Jsonobject = jsonobject. fromobject (jsonstring );
Return jsonobject. tobean (jsonobject, clazz );
}
/**
* Get a Java object from the character format of a JSON object. beanslist is a set of classes, such:
* {"ID": idvalue, "name": namevalue, "abean": {"abeanid": abeanidvalue ,...},
* Beanslist: [{},{},...]}
* @ Param jsonstring JSON character
* @ Param clazz converts the class of the Java object
* @ Param map? ? (Key: genus? ? Value? Type class. If this attribute is a set, it is the owner of the object in the set? Type) eg: ("beanslist": bean. Class)
* @ Return returns a Java object
*/
Public static object getdto (string jsonstring, class clazz, map ){
Jsonobject = NULL;
Setdataformat2java ();
Jsonobject = jsonobject. fromobject (jsonstring );
Return jsonobject. tobean (jsonobject, clazz, MAP );
}
/**
* Get the result from a JSON array? �� Java object array, such:
* [{"ID": idvalue, "name": namevalue}, {"ID": idvalue, "name": namevalue},...]
* @ Param jsonstring JSON character?
* @ Param clazz converts the class of the Java object
* @ Return Java object Array
*/
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 = array. getjsonobject (I );
OBJ [I] = jsonobject. tobean (jsonobject, clazz );
}
Return OBJ;
}
/**
* Get the result from a JSON array? �� Java object array, such:
* [{"ID": idvalue, "name": namevalue}, {"ID": idvalue, "name": namevalue},...]
* @ Param jsonstring JSON character?
* @ Param clazz converts the class of the Java object
* @ Param map? ? (Key: genus? ? Value? Type class. If this attribute is a set, it is the owner of the object in the set? Type)
* @ Return Java object Array
*/
Public static object [] getdtoarray (string jsonstring, class clazz, map ){
Setdataformat2java ();
Jsonarray array = jsonarray. fromobject (jsonstring );
Object [] OBJ = new object [array. Size ()];
For (INT I = 0; I <array. Size (); I ++ ){
Jsonobject = array. getjsonobject (I );
OBJ [I] = jsonobject. tobean (jsonobject, clazz, MAP );
}
Return OBJ;
}
/**
* Get the result from a JSON array? �� Java object set
* @ Param jsonstring JSON character?
* @ Param clazz converts the class of the Java object
* @ Return list containing Java objects
*/
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) ITER. Next ();
List. Add (jsonobject. tobean (jsonobject, clazz ));
}
Return list;
}
/**
* Get the result from a JSON array? �� Java object set, where the object contains the set owner?
* @ Param jsonstring JSON character?
* @ Param clazz converts the class of the Java object
* @ Param map? ? (Key: genus? ? Value? Type class. If this attribute is a set, it is the owner of the object in the set? Type)
* @ Return list containing Java objects
*/
Public static list getdtolist (string jsonstring, class clazz, map ){
Setdataformat2java ();
Jsonarray array = jsonarray. fromobject (jsonstring );
List list = new arraylist ();
For (iterator iter = array. iterator (); ITER. hasnext ();){
Jsonobject = (jsonobject) ITER. Next ();
List. Add (jsonobject. tobean (jsonobject, clazz, MAP ));
}
Return list;
}
/**
* Obtain the corresponding map from the JSON hash expression. This map supports the nesting function.
* Shape: {"ID": "johncon", "name": "Xiaoqiang "}
* Note that the commons-collections version must contain org. Apache. commons. Collections. Map. multikeymap.
* @ Param jsonstring JSON character?
* @ Return returns java. util. Map
*/
Public static map getmapfromjson (string jsonstring ){
Setdataformat2java ();
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 corresponding Java array from the json array (JSON is the basic data object type of Java
* JSON format: ["123", "456"]
* @ Param jsonstring JSON string
* @ Return Basic Data Object Array
*/
Public static object [] getobjectarrayfromjson (string jsonstring ){
Setdataformat2java ();
Jsonarray = jsonarray. fromobject (jsonstring );
Return jsonarray. toarray ();
}
/**
* Convert a data object to a JSON string
* DTO object: {"ID": idvalue, "name": namevalue ,...}
* Array objects, such as [{},...]
* Map objects such as {key1: {"ID": idvalue, "name": namevalue,...}, key2 :{},...}
* @ Param object any Java object
* @ Return a JSON string
*/
Public static string getjsonstring (Object object) throws exception {
String jsonstring = NULL;
Try {
// Date value Processor
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 if (Object instanceof string | object instanceof integer
| Object instanceof Boolean | object instanceof double | object instanceof float ){
Jsonstring = "{" + object. tostring () + "}";
} Else {
Jsonstring = jsonobject. fromobject (object, jsonconfig). tostring ();
}
}
} Catch (exception e ){
E. printstacktrace ();
}
Return jsonstring = NULL? "{}": Jsonstring;
}
Private Static void setdataformat2java (){
// Set the date conversion format
Jsonutils. getmorpherregistry (). registermorpher (New datemorpher (New String [] {"yyyy-mm-dd", "yyyy-mm-dd hh: mm: SS "}));
}
Public static void main (string [] Arg) throws exception {
List list = new arraylist ();
For (INT I = 0; I <10; I ++ ){
// Map = new hashmap ();
// Map. Put ("za", "");
// List. Add (MAP );
Map map = new hashmap ();
Map. Put ("A", "");
Map. Put ("B", new date ());
List. Add (MAP );
}
Jsonconfig = new jsonconfig ();
Jsonconfig. registerjsonvalueprocessor (Java. util. Date. Class, new jsondatevalueprocessor ());
Map map1 = getmapfromjson ("{\" B \ ": \" 22:18:35 \ ", \" A \ ": \" \"}");
System. Out. println (jsonobject. fromobject (new date (), jsonconfig). tostring ());
// Map M = new hashmap ();
// M. Put ("B", B. Class );
// M. Put ("map", map. Class );
// System. Out. println ("Object:" + jsonarray. fromobject (list). tostring ());
// A = new ();
// System. Out. println ("Object:" + jsonutil. getjsonstring (list ));
// List list11 = getdtolist (jsonutil. getjsonstring (list), map. Class );
// System. Out. println ("Object:" + list11.get (0 ));
// List list1 = jsonutil. getdtolist (jsonutil. getjsonstring (list), A. Class, M );
// For (iterator = list1.iterator (); iterator. hasnext ();){
// A = (a) iterator. Next ();
// System. Out. Print (A. getname () + ",");
// List list2 = A. getb ();
// For (iterator iterator2 = list2.iterator (); iterator2.hasnext ();){
// B = (B) iterator2.next ();
// System. Out. Print (B. getabc () + ",");
// System. Out. Print (B. getmap (). Get ("BS") + ",");
//}
// Map MA = A. getmap ();
// System. Out. println (MA. Get ("as"). tostring ());
//
//}
// System. Out. println ("AA ");
// A = (a) Class. forname ("com. IBM. CRl. idop. util. A"). newinstance ();
}
}