json對象使用

這裏使用 net.sf.json 包下面的json對象來操作數組以及對象格式。

首先引入依賴,注意這種方式,json-lib需要commons-beanutils-1.8.0.jar、commons-collections-3.2.1.jar、commons-lang-2.5.jar、commons-logging-1.1.1.jar、ezmorph-1.0.6.jar五個包的支持   

json-lib加上 <classifier>JDK15</classifier>

<dependency>
      <groupId>net.sf.json-lib</groupId>
      <artifactId>json-lib</artifactId>
      <version>2.3</version>
      <classifier>JDK15</classifier>
    </dependency>
    <dependency>
      <groupId>commons-beanutils</groupId>
      <artifactId>commons-beanutils</artifactId>
      <version>1.9.3</version>
    </dependency>
    <dependency>
      <groupId>commons-collections</groupId>
      <artifactId>commons-collections</artifactId>
      <version>3.2.2</version>
    </dependency>
    <dependency>
      <groupId>commons-lang</groupId>
      <artifactId>commons-lang</artifactId>
      <version>2.6</version>
    </dependency>
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.2</version>
    </dependency>
    <dependency>
      <groupId>net.sf.ezmorph</groupId>
      <artifactId>ezmorph</artifactId>
      <version>1.0.6</version>
    </dependency>

操作如下:

TbBrand brand = new TbBrand ();
        brand.setId (1L);
        brand.setName ("某某");
        brand.setFirstChar ("z");
        TbBrand brand2 = new TbBrand ();
        brand2.setId (1L);
        brand2.setName ("某某");
        brand2.setFirstChar ("z");

        //第一種,{....}對象格式
        JSONObject jsonObject = JSONObject.fromObject (brand);
        System.out.println ("第一種,{....}對象格式:"+jsonObject);
        //json轉對象
        TbBrand bean = (TbBrand)JSONObject.toBean (jsonObject,TbBrand.class);
        System.out.println ("json轉對象:"+bean.toString ());
        //第二種,[{...}]數組對象格式
        JSONArray jsonStr2 = JSONArray.fromObject (brand);
        System.out.println ("第二種,[{...}]數組對象格式:"+jsonStr2);

        //list對象轉json數組
        List list  = new ArrayList ();
        list.add (brand);
        list.add (brand2);
        JSONArray listJson = JSONArray.fromObject (list);
        System.out.println ("list對象轉json數組:"+listJson);

        //list對象轉json對象
        String jsonObj="{'brand':"+listJson.toString()+"}";
        System.out.println("list對象轉json對象:"+jsonObj);

        //json對象轉list對象
        TbBrand[] tbBrands = (TbBrand[]) JSONArray.toArray (listJson,TbBrand.class);
        for (TbBrand tbBrand : tbBrands) {
            System.out.println("json對象轉list對象:"+tbBrand);
        }

        //map對象轉換爲json
        Map<String,Object> map=new HashMap<String,Object> ();
        map.put("brand", brand);
        JSONObject jsonMap=JSONObject.fromObject(map);
        System.out.println ("map對象轉換爲json:"+jsonMap);

輸出結果:

第一種,{....}對象格式:{"firstChar":"z","name":"某某","id":1}
json轉對象:TbBrand{id=1, name='某某', firstChar='z'}
第二種,[{...}]數組對象格式:[{"firstChar":"z","name":"某某","id":1}]
list對象轉json數組:[{"firstChar":"z","name":"某某","id":1},{"firstChar":"z","name":"某某","id":1}]
list對象轉json對象:{'brand':[{"firstChar":"z","name":"某某","id":1},{"firstChar":"z","name":"某某","id":1}]}
json對象轉list對象:TbBrand{id=1, name='某某', firstChar='z'}
json對象轉list對象:TbBrand{id=1, name='某某', firstChar='z'}
map對象轉換爲json:{"brand":{"firstChar":"z","name":"某某","id":1}}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章