JSON使用方法舉例

來源:互聯網
上載者:User

標籤:style   blog   color   使用   os   2014   re   c   

例子1:

 1 import net.sf.json.JSONArray; 2 import net.sf.json.JSONObject; 3  4 /** 5  * Created by leiqiang on 2014/7/25. 6  */ 7 public class JSONObjectSample { 8     // 建立JSONObject對象 9     private static JSONObject createJSONObject() {10         JSONObject jsonObject = new JSONObject();11         jsonObject.put("username", "leiqiang");12         jsonObject.put("sex", "男");13         jsonObject.put("nickname", "lqPROplus");14         return jsonObject;15     }16 17     public static void main(String[] args) {18         JSONObject jsonObject = JSONObjectSample.createJSONObject();//靜待方法,直接通過類名+方法調用19         // 輸出jsonobject對象20         System.out.println("jsonObject:" + jsonObject);21 22         // 判讀輸出對象的類型23         boolean isArray = jsonObject.isArray();24         boolean isEmpty = jsonObject.isEmpty();25         boolean isNullObject = jsonObject.isNullObject();26         System.out.println("是否為數組:" + isArray + ", 是否為空白:" + isEmpty27                 + ", isNullObject:" + isNullObject);28 29         // 添加屬性,在jsonObject後面追加元素。30         jsonObject.element("address", "北京市");31         System.out.println("添加屬性後的對象:" + jsonObject);32 33         // 返回一個JSONArray對象34         JSONArray jsonArray = new JSONArray();35         jsonArray.add(0, "this is a jsonArray value");36         jsonArray.add(1, "another jsonArray value");37         jsonObject.element("jsonArray", jsonArray);38         //在jsonObject後面加一個jsonArray39         JSONArray array = jsonObject.getJSONArray("jsonArray");40         //System.out.println("加入jsonArray後的對象:"+jsonObject);41 42 43         System.out.println("返回一個JSONArray對象:" + array);44         // 添加JSONArray後的值45         System.out.println("結果=" + jsonObject);46 47         // 根據key返回一個字串48         String username = jsonObject.getString("username");49         System.out.println("username=" + username);50 51         // 把字元轉換為 JSONObject52         String temp = jsonObject.toString();53         JSONObject object = JSONObject.fromObject(temp);54 55         // 轉換後根據Key傳回值56         System.out.println("nickname=" + object.get("nickname"));57 58     }59 }

運行結果:

1 jsonObject:{"username":"leiqiang","sex":"男","nickname":"lqPROplus"}2 是否為數組:false, 是否為空白:false, isNullObject:false3 添加屬性後的對象:{"username":"leiqiang","sex":"男","nickname":"lqPROplus","address":"北京市"}4 返回一個JSONArray對象:["this is a jsonArray value","another jsonArray value"]5 結果={"username":"leiqiang","sex":"男","nickname":"lqPROplus","address":"北京市","jsonArray":["this is a jsonArray value","another jsonArray value"]}6 username==>leiqiang7 nickname=lqPROplus

例子2:

 1 import net.sf.json.JSONArray; 2 import net.sf.json.JSONObject; 3  4 /** 5  * Created by leiqiang on 2014/7/25. 6  */ 7 public class JSONTest { 8     public static void main(String args[]) { 9         JSONObject jsonObj0 = new JSONObject();10         JSONObject jsonObj1 = new JSONObject();11         JSONObject jsonObj2 = new JSONObject();12         JSONObject jsonObj3 = new JSONObject();13         JSONArray jsonArray = new JSONArray();14 15         //建立jsonObj016         jsonObj0.put("name0", "zhangsan");17         jsonObj0.put("sex1", "female");18         System.out.println("jsonObj0:" + jsonObj0);19 20         //建立jsonObj121         jsonObj1.put("name", "lisi");22         jsonObj1.put("sex", "male");23         System.out.println("jsonObj:" + jsonObj1);24 25         //建立jsonObj2,包含兩個條目,條目內容分別為jsonObj0,jsonObj126         jsonObj2.put("item0", jsonObj0);27         jsonObj2.put("item1", jsonObj1);28         System.out.println("jsonObj2:" + jsonObj2);29 30         //建立jsonObj3,只有一個條目,內容為jsonObj231         jsonObj3.element("j3", jsonObj2);32         System.out.println("jsonObj3:" + jsonObj3);33 34         //往JSONArray中添加JSONObject對象。發現JSONArray跟JSONObject的區別就是JSONArray比JSONObject多中括弧[]35         jsonArray.add(jsonObj1);36         System.out.println("jsonArray:" + jsonArray);37 38         JSONObject jsonObj4 = new JSONObject();39         jsonObj4.element("weather", jsonArray);40         System.out.println("jsonObj4:" + jsonObj4);41     }42 }

運行結果:

1 jsonObj0:{"name0":"zhangsan","sex1":"female"}2 jsonObj:{"name":"lisi","sex":"male"}3 jsonObj2:{"item0":{"name0":"zhangsan","sex1":"female"},"item1":{"name":"lisi","sex":"male"}}4 jsonObj3:{"j3":{"item0":{"name0":"zhangsan","sex1":"female"},"item1":{"name":"lisi","sex":"male"}}}5 jsonArray:[{"name":"lisi","sex":"male"}]6 jsonObj4:{"weather":[{"name":"lisi","sex":"male"}]}

例子3:

 1 import net.sf.json.JSONObject; 2  3 /** 4  * Created by leiqiang on 2014/7/25. 5  */ 6 public class JSONfromObject { 7     public static void main(String[] args) { 8  9         JSONObject object = JSONObject.fromObject("{\"username\":\"leiqiang\",\"sex\":\"男\",\"nickname\":\"lqPROplus\"}");10         System.out.println("nickname=" + object.get("nickname"));11 12         System.out.println(object.getString("username"));13 14         JSONObject object1 = new JSONObject();15         object1.put("a",object);16         System.out.println(object1);17     }18 19 }

運行結果:

1 nickname=lqPROplus2 leiqiang3 {"a":{"username":"leiqiang","sex":"男","nickname":"lqPROplus"}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.