//1.封装集合
List list = new ArrayList();
list.add( "first" );
list.add( "second" );
JSONArray jsonArray1 = JSONArray.fromObject( list );
System.out.println("集合:"+jsonArray1);
//2.封装数组
int[] Array ={ 1, 2, 3 };
JSONArray jsonArray2 = JSONArray.fromObject(Array);
System.out.println("数组:"+jsonArray2);
//3.封装一般数据
JSONArray jsonArray3 = JSONArray.fromObject("['json','is','easy']" );
System.out.println("一般数据:"+jsonArray3);
//3.封装map
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 jsonObject1 = JSONObject.fromObject(map);
System.out.println("map:"+jsonObject1);
//3.封装对象
Student student=new Student();
student.setAge(20);
student.setName("张三");
JSONObject jsonObject2 =JSONObject.fromObject(student);
String jsonString=jsonObject2.toString();
//解析json
JSONObject jsonObject3 =JSONObject.fromObject(jsonString);
List list = new ArrayList();
list.add( "first" );
list.add( "second" );
JSONArray jsonArray1 = JSONArray.fromObject( list );
System.out.println("集合:"+jsonArray1);
//2.封装数组
int[] Array ={ 1, 2, 3 };
JSONArray jsonArray2 = JSONArray.fromObject(Array);
System.out.println("数组:"+jsonArray2);
//3.封装一般数据
JSONArray jsonArray3 = JSONArray.fromObject("['json','is','easy']" );
System.out.println("一般数据:"+jsonArray3);
//3.封装map
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 jsonObject1 = JSONObject.fromObject(map);
System.out.println("map:"+jsonObject1);
//3.封装对象
Student student=new Student();
student.setAge(20);
student.setName("张三");
JSONObject jsonObject2 =JSONObject.fromObject(student);
String jsonString=jsonObject2.toString();
//解析json
JSONObject jsonObject3 =JSONObject.fromObject(jsonString);
System.out.println("姓名:"+jsonObject3.get("name")+"\t"+"年龄:"+jsonObject3.get("age"));
JAR包简介
1.commons-lang.jar
2.commons-beanutils.jar
3.commons-collections.jar
4.commons-logging.jar
5.ezmorph.jar
6.json-lib-2.2.2-jdk15.jar