json的使用簡介

openkk 12年前發布 | 2K 次閱讀 5.2.1版本發布

json 所依賴的jar包   均可在 jar114.com 下載到

  1. commons-beanutils.jar
  2. commons-collections-3.2.1.jar
  3. commons-lang-2.5.jar
  4. commons-logging-1.1.1.jar
  5. ezmorph-1.0.6.jar
  6. json-lib-2.2.3-jdk15.jar


    一般使用方法
    </span>


    JSONArray jsonArray = JSONArray.fromObject(getList());

    System.out.println(jsonArray);


     轉化數組和集合
        

    boolean[] boolArray = new boolean[]{true,false,true};
           JSONArray jsonArray = JSONArray.fromObject(boolArray);
           System.out.println(jsonArray);
       輸出:[true,false,true]
        List list = new ArrayList();
           list.add(“第一個”);
           list.add(“第二個”);
           JSONArray jsonArray = JSONArray.fromObject(list);
        System.out.println(jsonArray);
       輸出:[“第一個”, “第二個”]
          
           JSONArray jsonArray3 = JSONArray.fromObject("['json','is','easy']");
        System.out.println(jsonArray3);
       輸出:[“json”, “is”, “'easy'”]
      

    轉化對象

    轉化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 json = JSONObject.fromObject(map);
    System.out.println(json);
    輸出:[“name”: “json”, “bool”:true, “int”,1, “arr”:[ “a”, “b”], “func”:function(i){return this.arr[i];}]

    轉化Bean

    MyBean.java
    public class MyBean {
        private String name = "json";
      private int pojoId = 1;
        private String func1 = "function(i){return this.options[i]}";
        private JSONFunction func2 = new JSONFunction(new String[]{"i"},"return this.options[i];");
       
        //以下為get、set方法
    }
    MyBean bean = new MyBean();
    JSONObject jsonObject = JSONObject.fromObject(bean);
    System.out.println(jsonObject);
    輸出:
    {"func1":function(i){return this.options[i]},"pojoId":1,"name":"json","func2":function(i){ return this.options[i]; }

    從JSON到Beans

    //轉化為動態bean
    String myjson = "{name=\"json\",bool:true,int:1,double:2.2,function:function(a){return a;},array:[1,2]}";
    JSONObject json1 = JSONObject.fromString(myjson);
    Object bean1 = JSONObject.toBean(json1);
    由JSON生成XML
    JSONObject json = new JSONObject(true);
    XMLSerializer xmlSerializer = new XMLSerializer();
    String xml = xmlSerializer.write(json);
    System.out.println("xml:" + xml);
    輸出:xml:<?xml version="1.0" encoding="UTF-8"?>
    <o null="true"/>
    JSONObject json2 = JSONObject.fromObject("{\"name\":\"json\",\"bool\":true,\"int\":1}");
    String xml2 = xmlSerializer.write(json2);
    System.out.println("xml2:" + xml2);
    輸出:xml2:<?xml version="1.0" encoding="UTF-8"?>
    <o><bool type="boolean">true</bool><int type="number">1</int><name type="string">json</name></o>
    JSONArray json3 = JSONArray.fromObject("[1,2,3]");
    String xml3 = xmlSerializer.write(json3);
    System.out.println("xml3:" + xml3);
    輸出:xml3:<?xml version="1.0" encoding="UTF-8"?>
    <a><e type="number">1</e><e type="number">2</e><e type="number">3</e></a>
 本文由用戶 openkk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!