JSON LIB 简

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("#################################################");
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值