1. json-lib is a java class library that provides the function of converting Java objects, including beans, maps, collections, java arrays and XML, into JSON, or reverse conversion.
2. json-lib homepage: http://json-lib.sourceforge.net/
3. Execution environment
The following library support is required
commons-lang 2.5
commons-beanutils 1.8.0
commons-collections 3.2.1
commons-logging 1.1.1
ezmorph 1.0.6
4. Functional examples
Here is a code example given by JUnit-Case example
The code copy is as follows:
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 {
/*
* Normal types, List, Collection, etc. are all parsed using JSONArray
*
* Map and custom types are parsed using JSONObject
* Map can be understood as an object, and the key/value pairs inside can be understood as the object's attributes/attribute values
* That is {key1:value1,key2,value2...}
*
* 1. JSONObject is a name:values collection. The corresponding value part (string) after the key is obtained through its get(key) method.
* Through its getJSONObject(key), you can get a JSONObject, --> convert it to map,
* Through its getJSONArray(key) you can get a JSONArray,
*
*
*/
//Generally convert arrays to JSON
@Test
public void testArrayToJSON(){
boolean[] boolArray = new boolean[]{true,false,true};
JSONArray jsonArray = JSONArray.fromObject( boolArray );
System.out.println( jsonArray );
// prints [true,false,true]
}
//Convert the Collection object to JSON
@Test
public void testListToJSON(){
List list = new ArrayList();
list.add( "first" );
list.add( "second" );
JSONArray jsonArray = JSONArray.fromObject( list );
System.out.println( jsonArray );
// prints ["first","second"]
}
//Convert string json to json, depending on the situation, use JSONArray or JSONObject
@Test
public void testJsonStrToJSON(){
JSONArray jsonArray = JSONArray.fromObject( "['json','is','easy']" );
System.out.println( jsonArray );
// prints ["json","is","easy"]
}
// Convert Map to json, using jsonObject
@Test
public void testMapToJSON(){
Map 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 = JSONObject.fromObject( map );
System.out.println( jsonObject );
}
//Compound type bean into 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("Agricultural Bank");
cardNum.add("ICBC");
cardNum.add("CCB");
cardNum.add(new Person("test"));
bean.setCardNum(cardNum);
JSONObject jsonObject = JSONObject.fromObject(bean);
System.out.println(jsonObject);
}
//Convert json of normal type into object
@Test
public void testJSONToObject() throws Exception{
String json = "{name=/"json/",bool:true,int:1,double:2.2,function: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);
}
}
//Parse json into a composite type 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 type object, including Map
@Test
public void testJSONToBeanHavaMap(){
//Think of Map as an object
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);
//Use hint, directly parse json into a specified custom object, where List is fully parsed, Map is not fully parsed
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 transformer in the registry first, and you need to use the class in the ezmorph package
MorpherRegistry morpherRegistry = JSONUtils.getMorpherRegistry();
Morpher dynaMorpher = new BeanMorpher( Person.class, morpherRegistry);
morpherRegistry.registerMorpher( dynaMorpher );
Map map = diyBean.getMap();
/*The map here does not have type hints, so according to the default, the stored object is a type net.sf.ezmorph.bean.MorphDynaBean*/
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 registrar to perform object transformation 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 following provides the resources required for the above example, including jar packages and code
/Files/mailingfeng/json-lib/json-lib use case requires jar packages and java classes.rar