1. 需要导入的jar有:
commons-beanutils-1.8.0.jar不加这个包
java.lang.NoClassDefFoundError: org/apache/commons/beanutils/DynaBean
commons-collections.jar 不加这个包
java.lang.NoClassDefFoundError: org/apache/commons/collections/map/ListOrderedMap
commons-lang-2.4.jar不加这个包
java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntimeException
commons-logging-1.1.1.jar不加这个包
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
ezmorph-1.0.4.jar不加这个包
java.lang.NoClassDefFoundError: net/sf/ezmorph/Morpher
json-lib-2.3-jdk15.jar不加这个包
java.lang.NoClassDefFoundError: net/sf/json/JSONObject
java.lang.NoClassDefFoundError: org/apache/commons/beanutils/DynaBean
commons-collections.jar 不加这个包
java.lang.NoClassDefFoundError: org/apache/commons/collections/map/ListOrderedMap
commons-lang-2.4.jar不加这个包
java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntimeException
commons-logging-1.1.1.jar不加这个包
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
ezmorph-1.0.4.jar不加这个包
java.lang.NoClassDefFoundError: net/sf/ezmorph/Morpher
json-lib-2.3-jdk15.jar不加这个包
java.lang.NoClassDefFoundError: net/sf/json/JSONObject
package ray.json;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class JSon {
public static JSONObject object = new JSONObject();
public static void main(String[] args) {
testList();
testMap();
testBean();
testArray();
testCommon();
testObject();
}
//封装到JsonObject中,在需要的地方解析Json即可
private static void testObject() {
System.out.println(object);
}
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "JSon [id=" + id + ", name=" + name + "]";
}
//一般数据转换成json代码
private static void testCommon() {
JSONArray jsonAry = JSONArray.fromObject("['Uno','is', '梓诺的', 'father']" );
//["Uno","is","梓诺的","father"]
System.out.println(jsonAry);
object.put("jsonCommonAry", jsonAry);
}
//数组转换成json代码
private static void testArray() {
boolean[] ary = new boolean[] { true, false, true };
JSONArray jsonAry = JSONArray.fromObject(ary);
//[true,false,true]
System.out.println(jsonAry);
object.put("jsonAry", jsonAry);
}
//Bean转为Json,注意这个Bean一定要是public的class,否则抛异常
private static void testBean() {
JSon bean = new JSon();
bean.setId(1);
bean.setName("雷梓诺");
JSONObject jsonObject = JSONObject.fromObject(bean);
//{"id":1,"name":"雷梓诺"}
System.out.println(jsonObject);
object.put("Bean", jsonObject);
}
//Map集合转换成json方法
private static void testMap() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", "雷梓诺");
map.put("bool", Boolean.TRUE);
map.put("int", null);
map.put("arr", new String[] { "a", "b" });
// map.put(null, null);//JSON keys cannot be null.
map.put("func", "function(i){ return this.arr[i]; }");
JSONObject json = JSONObject.fromObject(map);
//{"arr":["a","b"],"int":null,"name":"雷梓诺","func":function(i){ return this.arr[i]; },"bool":true}
System.out.println(json);
object.put("jsonMap", json);
}
//List集合转换成json方法
private static void testList() {
List<String> list = new ArrayList<String>();
list.add( "first" );
list.add( "second" );
JSONArray ary = JSONArray.fromObject( list );
System.out.println(ary);//["first","second"]
object.put("list", ary);
}
}