Package COM. mai. JSON; import static Org. JUnit. assert. assertequals; import Java. util. arraylist; import Java. util. date; import Java. util. hashmap; import Java. util. iterator; import Java. util. list; import Java. util. map; import net. SF. ezmorph. morpher; import net. SF. ezmorph. morpherregistry; import net. SF. ezmorph. bean. beanmorpher; import net. SF. JSON. jsonarray; import net. SF. JSON. jsonobject; import net. SF. JSON. util. jsonutils; import Org. apache. commons. beanutils. propertyutils; import Org. JUnit. test; public class jsonlibtest {/** common type, list, collection, etc. are all parsed using jsonarray ** map, custom type is parsed using jsonobject * map can be understood as an object, the key/value pair can be understood as the property/attribute value * of the object, that is, {key1: value1, key2, value2 ......} ** 1. jsonobject is a name: values set. The get (key) method obtains the value part (string) corresponding to the key * through its getjsonobject (key) you can get a jsonobject, --> to map, * through its getjsonarray (key), you can get a jsonarray, * ** // convert a general array to JSON @ test public void testarraytojson () {Boolean [] boolarray = new Boolean [] {true, false, true }; jsonarray = jsonarray. fromobject (boolarray); system. out. println (jsonarray); // prints [true, false, true]} // convert the collection object to JSON @ test public void testlisttojson () {list = new arraylist (); list. add ("first"); list. add ("second"); jsonarray = jsonarray. fromobject (list); system. out. println (jsonarray); // prints ["first", "second"]} // converts the string JSON to JSON. Use jsonarray or jsonobject @ test public void testjsonstrtojson () based on the situation () {jsonarray = jsonarray. fromobject ("['json', 'is ', 'easy']"); system. out. println (jsonarray); // prints ["JSON", "is", "easy"]} // map is converted to JSON, which uses jsonobject @ test public void testmaptojson () {map = new hashmap (); map. put ("name", "JSON"); map. put ("bool", Boolean. true); map. put ("int", new INTEGER (1); map. put ("arr", new string [] {"A", "B"}); map. put ("func", "function (I) {return this. arr [I];} "); jsonobject = jsonobject. fromobject (MAP); system. out. println (jsonobject);} // convert the composite bean to JSON @ test public void testbeadtojson () {mybean bean = new mybean (); bean. setid ("001"); bean. setname ("bank card"); bean. setdate (new date (); List cardnum = new arraylist (); cardnum. add ("ABC"); cardnum. add ("ICBC"); cardnum. add (""); cardnum. add (new person ("test"); bean. setcardnum (cardnum); jsonobject = jsonobject. fromobject (bean); system. out. println (jsonobject);} // convert a common JSON type to an object @ test public void testjsontoobject () throws exception {string JSON = "{name = \" JSON \ ", bool: true, INT: 1, double: 2.2, FUNC: function (a) {return a ;}, array: []} "; jsonobject = jsonobject. fromobject (JSON); system. out. println (jsonobject); object bean = jsonobject. tobean (jsonobject); assertequals (jsonobject. get ("name"), propertyutils. getproperty (bean, "name"); assertequals (jsonobject. get ("bool"), propertyutils. getproperty (bean, "bool"); assertequals (jsonobject. get ("int"), propertyutils. getproperty (bean, "int"); assertequals (jsonobject. get ("double"), propertyutils. getproperty (bean, "double"); assertequals (jsonobject. get ("func"), propertyutils. getproperty (bean, "func"); system. out. println (propertyutils. getproperty (bean, "name"); system. out. println (propertyutils. getproperty (bean, "bool"); system. out. println (propertyutils. getproperty (bean, "int"); system. out. println (propertyutils. getproperty (bean, "double"); system. out. println (propertyutils. getproperty (bean, "func"); system. out. println (propertyutils. getproperty (bean, "array"); List arraylist = (list) jsonarray. tocollection (jsonobject. getjsonarray ("array"); For (Object object: arraylist) {system. out. println (object) ;}// parses JSON into a composite object, including list @ test public void testjsontobeanhavalist () {string JSON = "{list: [{Name: 'test1'}, {Name: 'test2'}], MAP: {test1: {Name: 'test1'}, Test2: {Name: 'test2 '}}}"; // string JSON = "{list: [{Name: 'test1'}, {Name: 'test2'}]}"; Map classmap = new hashmap (); classmap. put ("list", person. class); mybeanwithperson diybean = (mybeanwithperson) jsonobject. tobean (jsonobject. fromobject (JSON), mybeanwithperson. class, classmap); system. out. println (diybean); List list = diybean. getlist (); For (Object O: List) {If (O instanceof person) {person P = (person) O; system. out. println (P. getname () ;}}// parse JSON into a composite object, including map @ test public void testjsontobeanhavamap () {// regard map as a string JSON = "{list: [{Name: 'test1'}, {Name: 'test2'}], MAP: {testone: {Name: 'test1'}, testtwo: {Name: 'test2' }}} "; Map classmap = new hashmap (); classmap. put ("list", person. class); classmap. put ("map", map. class); // usage suggestion: JSON is directly parsed as a specified custom object, in which list is completely parsed, and map is not completely parsed as mybeanwithperson diybean = (mybeanwithperson) jsonobject. tobean (jsonobject. fromobject (JSON), mybeanwithperson. class, classmap); system. out. println (diybean); system. out. println ("do the list release"); List <person> List = diybean. getlist (); For (person o: List) {person P = (person) O; system. out. println (P. getname ();} system. out. println ("do the map release"); // register the converter in the register first, and use the class morpherregistry = jsonutils in the ezmorph package. getmorpherregistry (); morpher dynamorpher = new beanmorpher (person. class, morpherregistry); morpherregistry. registermorpher (dynamorpher); map = diybean. getmap ();/* the map here does not imply the type, so it is saved as net by default. SF. ezmorph. bean. morphdynabean object */system. out. println (MAP);/* output: {testone = net. SF. ezmorph. bean. morphdynabean @ f73c1 [{name = test1}], testtwo = net. SF. ezmorph. bean. morphdynabean @ 186c6b2 [{name = Test2}]} */list <person> output = new arraylist (); For (iterator I = map. values (). iterator (); I. hasnext ();) {// use the Register to perform object conversion for the specified dynabean. output. add (person) morpherregistry. morph (person. class, I. next ();} For (person P: Output) {system. out. println (P. getname ();/* output: test1 Test2 */}}}