JSON LIB依赖的JAR包在附件,以下是小练习:
package json;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
* 两种核心JSON格式:对象格式和数组格式[List只能转成JSONArray而不能转为JSONObject]
* JSONObject------{xx:xx,yy:yy}
* JSONArray-------[{xx:xx,yy:yy},..]
* @author edison.seven
*/
public class Boot {
private static Student student = null;
private static Group group = null;
public static void main(String[] args) throws Exception{
Boot boot = new Boot();
//step1:object===>jsonObject
// boot.object2json();
//step2:list=====>jsonArray
// boot.list2json();
//step3:map======>jsonArray
// boot.map2json();
//step4:string===>jsonObject
// boot.str2jsonObject();
//step5:string===>jsonArray
// boot.str2jsonArray();
//step6:json=====>object
// boot.json2object();
//step7:json=====>list
// boot.json2list();
//step8:json=====>map
boot.json2map();
}
/**
* object2jsonObject
* JSONObject.fromObject(bean)---Object===>JSONObject
* fromObject将Java对象转换成json字符串,toBean将json对象转换成Java对象
*/
public void object2json(){
//JavaObject==>JsonObject 所转换的属性-依赖于属性是否有getXX方法
log(JSONObject.fromObject(student));
//JavaObject===>JsonArray
log(JSONArray.fromObject(student));
}
public void list2json() throws CloneNotSupportedException{
List studentlist = new ArrayList();
//因只看转换效果,此处用clone,以免创建多个实例
studentlist.add(student.clone());
studentlist.add(student.clone());
studentlist.add(student.clone());
studentlist.add(student.clone());
//list==>JsonArray
log(JSONArray.fromObject(studentlist));
}
public void map2json() throws CloneNotSupportedException{
Map studentMap = new LinkedHashMap();
studentMap.put("s1",student.clone());
studentMap.put("s2",student.clone());
studentMap.put("s3",student.clone());
studentMap.put("s4",student.clone());
//list==>JsonArray
log(JSONArray.fromObject(studentMap));
//map===>JsonObject
log(JSONObject.fromObject(studentMap));
}
//String=====>jsonObject
public void str2jsonObject(){
String jsonStr = JSONObject.fromObject(student).toString();
log(JSONObject.fromObject(jsonStr));
}
//String=====>jsonArray
public void str2jsonArray() throws Exception{
List studentlist = new ArrayList();
studentlist.add(student.clone());
studentlist.add(student.clone());
studentlist.add(student.clone());
studentlist.add(student.clone());
String jsonStr = JSONArray.fromObject(studentlist).toString();
log(JSONArray.fromObject(jsonStr));
}
public void json2object(){
String jsonStr = JSONObject.fromObject(student).toString();
//string====>JsonObject
Student s = (Student)JSONObject.toBean(JSONObject.fromObject(jsonStr), Student.class);
log(s);
}
public void json2list() throws Exception{
List studentlist = new ArrayList();
studentlist.add(student.clone());
studentlist.add(student.clone());
studentlist.add(student.clone());
studentlist.add(student.clone());
String jsonStr = JSONArray.fromObject(studentlist).toString();
//string====>JsonArray
JSONArray jsonArray = JSONArray.fromObject(jsonStr);
//JsonArray===>Collection
Collection c = JSONArray.toCollection(jsonArray, Student.class);
//转为JavaObject
for(Object o : c){
log(((Student)o).toString());
}
// //#####################################################################
div();
//或JsonArray===>Student[]
Student[] ss = (Student[])JSONArray.toArray(jsonArray,Student.class);
//Student[]===>list
List list = Arrays.asList(ss);
for(Object o : list){
log(o.getClass().getName() + "!!!!!" + o.toString());
}
}
public void json2map() throws Exception{
Map studentMap = new LinkedHashMap();
studentMap.put("s1",student.clone());
studentMap.put("s2",student.clone());
studentMap.put("s3",student.clone());
studentMap.put("s4",student.clone());
//map===>string
String json = JSONObject.fromObject(studentMap).toString();
//string===>JSONObject
JSONObject jsonObject = JSONObject.fromObject(json);
//定义(KEY指定的)VALUE-class映射
Map clazzMapping = new HashMap();
//映射-可用正则表达式--此处表示JSON中所有的对象都以student类型转存至Map中
clazzMapping.put(".*", Student.class);
//params:JsonObject对象|转存为Bean的类型|Map中各对象值的类型映射
Map map = (Map)JSONObject.toBean(jsonObject, LinkedHashMap.class,clazzMapping);
Set set = map.keySet();
for(Object obj : set){
log(((Student)map.get(obj)).toString());
}
}
//////////////////////////////////////////////////////////////////////////////////////////
public Boot(){
student = new Student();
student.setId("1");
student.setName("xiaoming");
student.setGender("male");
student.setAge("100");
group = new Group();
group.setGroupID("11");
group.setGroupName("一班");
student.setGroup(group);
}
public void log(Object o){
System.out.println("========>" + o);
}
public void div(){
System.out.println("#################################################");
}
}