1. Json-lib is a Java class library that provides the ability to convert Java objects, including beans, maps, collections, Java Arrays and XML, into JSON, or reverse conversion.
2. Json-lib Home: http://json-lib.sourceforge.net/
3. Implementation environment
The following class library support is required
Jakarta Commons-lang 2.5
Jakarta commons-beanutils 1.8.0
Jakarta commons-collections 3.2.1
Jakarta commons-logging 1.1.1
Ezmorph 1.0.6
4. Function Example
Here is a sample of the code through the Junit-case example
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; The public class Jsonlibtest {/* * common Type, List, collection, etc. are all jsonarray resolved * * Map, custom type is JSON
Object Parsing * Can interpret a map as an object in which the key/value can be interpreted as an object's attribute/attribute value * i.e. {key1:value1,key2,value2 ...} * * 1.JSONObject is a name:values set, through its get (key) method is the corresponding value of the key (string) * through its getjsonobject (key) can be taken to a Jsonobject,--> converted to map, * through its getjsonarray (key) can be taken to a jsonarray, *
*//General array converted to JSON @Test public void Testarraytojson () {boolean[] bool
Array = new Boolean[]{true,false,true};
Jsonarray Jsonarray = Jsonarray.fromobject (Boolarray);
System.out.println (Jsonarray); Prints [true,false,true]}//collection object converted to JSON @Test public void Test
Listtojson () {List List = new ArrayList ();
List.add ("a");
List.add ("second");
Jsonarray Jsonarray = jsonarray.fromobject (list);
System.out.println (Jsonarray); Prints ["A", "second"]}//String JSON is converted to JSON, depending on whether the Jsonarray or Jsonobject @Tes
T public void Testjsonstrtojson () {Jsonarray Jsonarray = Jsonarray.fromobject ("[' json ', ' are ', ' easy ']");
System.out.println (Jsonarray);
Prints [' json ', ' is ', ' Easy ']}
The map is converted to JSON using the Jsonobject @Test public void Testmaptojson () {map map = new Hash
Map ();
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 = jsonobject.fromobject (map);
System.out.println (Jsonobject);
The//composite type Bean is converted 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 ("CCB");
Cardnum.add (New person ("test"));
Bean.setcardnum (Cardnum);Jsonobject jsonobject = Jsonobject.fromobject (bean);
System.out.println (Jsonobject); }//Normal type JSON converted to object @Test public void Testjsontoobject () throws exception{String JS
On = ' {name=\ ' json\ ', Bool:true,int:1,double:2.2,func:function (a) {return A;},array:[1,2]} ';
Jsonobject 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 type object that contains the list @Test public void Testjsontobean Havalist () {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 ()); Resolves JSON to a composite type object containing the map @Test public void Testjsontobeanhava Map () {//Map as an object String json = ' {list:[{name: ' test1 '},{name: ' test2 '}],map:{testone:{name: ' test1 '},t
Esttwo:{name: ' Test2 '}} ';
Map Classmap = new HashMap ();
Classmap.put ("list", Person.class);
Classmap.put ("Map", Map.class); Use hints to parse JSON directly into a specified custom object, where list is fully parsed and map is not fully parsed//view more highlights in this column: http://www.bianceng.cnhttp://www.bianceng.cn/ Programming/java/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 first to register, need to use the class morpherregistry morpherregistry = Jsonutils.getmorpherregistry () in the Ezmorph package;
Morpher dynamorpher = new Beanmorpher (Person.class, morpherregistry);
Morpherregistry.registermorpher (Dynamorpher);
Map map = Diybean.getmap ();
/* The map here does not carry type hint, so by default, it is stored as Net.sf.ezmorph.bean.MorphDynaBean type of object/System.out.println (map); /* Output: {testone=net.sf.ezmorph.bean.morphdynabean@f73c1[{name=test1}], testtwo=net.sf.ezmorph.bean.morphdynabean@186c6b2[{name=test2}]} */L
ist<person> output = new ArrayList (); for (Iterator i = map.values (). iterator (); I.hasnext ();) {///using the Registrar to make object transformations on the specified Dynabean output.add (person) morpherregistry.morph (Person.class, I.next ()
) );
for (person P:output) {System.out.println (P.getname ()); /* Output: Test1 test2 */}}}
5. The resources required for the above examples are provided below, including Jar packs and code
Jar packages and Java classes required for/files/mailingfeng/json-lib/json-lib use cases. rar
http://download.csdn.net/detail/shimiso/7058455