JSON簡單例子






1
JSONObject aa = new JSONObject(); 2 aa.put("username","huangwuyi"); 3 aa.put("sex", "男"); 4 aa.put("QQ", "9999999"); 5 aa.put("Min.score", new Integer(99)); 6 aa.put("nickname", "夢中心境"); 7 return aa;

創建一個JSON數據。

1         JSONObject bb = Main.creat();
2         //輸出jsonobject對象
3         System.out.println("添加屬性前的對象jsonObject==>\n" + bb);

用main方法中創建一個新的JSON數據組,輸出得到:

判斷輸出對象的具體類型:

1         //判讀輸出對象的類型
2         boolean isArray = bb.isArray();  //判斷是否為數組
3         boolean isEmpty = bb.isEmpty();   //判斷是否為空
4         boolean isNullObject = bb.isNullObject();  //判斷是否具有有效數據
5         System.out.println("isArray:" + isArray + " isEmpty:" + isEmpty + " isNullObject:" + isNullObject);

得到結果:

用.element()方法插入數據:

1         bb.element("address", "北京");
2         System.out.println("添加屬性后的對象==>\n" + bb);

得到結果:

添加一各json數據進入:

1         //返回一個JSONArray對象
2         JSONArray jsonArray = new JSONArray();
3         jsonArray.add(0, "white");//插入
4         jsonArray.add(1, "friend");//插入
5         bb.element("jsonArray", jsonArray);//bb中插入jsonArray
6         JSONArray array = bb.getJSONArray("jsonArray");
7         System.out.println("返回對象==>\n" + array);
8         //添加JSONArray后的值
9         System.out.println("結果==>\n" + bb);

得到結果:

嘗試根據key值返回字符串:

1         //根據key返回一個字符串
2         String byp = bb.getString("sex");
3         System.out.println("111"+ byp);

得到結果如下:

將字符轉換為JSONObject:

1         // 把字符轉換為 JSONObject
2         String temp = bb.toString();
3         JSONObject object = JSONObject.fromObject(temp);
4         System.out.println(object);

得到結果:

根據key值提取屬性值:

1         //轉換后根據Key返回值
2         System.out.println("qq==>" + object.get("QQ"));

得到結果:

全部代碼:

 1 import net.sf.json.JSONArray;
 2 import net.sf.json.JSONObject;
 3 
 4 public class Main {
 5     //創建JSONObject對象
 6     private static JSONObject creat() {
 7         JSONObject aa = new JSONObject();
 8         aa.put("username","BAI");
 9         aa.put("sex", "男");
10         aa.put("QQ", "8724911111");
11         aa.put("Min.score", new Integer(10));
12         aa.put("name", "花花");
13         return aa;
14 
15     }
16 
17     public static void main(String[] args) {
18         JSONObject bb = Main.creat();
19         //輸出jsonobject對象
20         System.out.println("添加屬性前的對象jsonObject==>\n" + bb);
21         //判讀輸出對象的類型
22         boolean isArray = bb.isArray();
23         boolean isEmpty = bb.isEmpty();
24         boolean isNullObject = bb.isNullObject();
25         System.out.println("isArray:" + isArray + " isEmpty:" + isEmpty + " isNullObject:" + isNullObject);
26           //添加屬性
27         bb.element("address", "北京");
28         System.out.println("添加屬性后的對象==>\n" + bb);
29 
30         //返回一個JSONArray對象
31         JSONArray jsonArray = new JSONArray();
32         jsonArray.add(0, "white");//插入
33         jsonArray.add(1, "friend");//插入
34         bb.element("jsonArray", jsonArray);//bb中插入jsonArray
35         JSONArray array = bb.getJSONArray("jsonArray");
36         System.out.println("返回對象==>\n" + array);
37         //添加JSONArray后的值
38         System.out.println("結果==>\n" + bb);
39 
40         //根據key返回一個字符串
41         String byp = bb.getString("sex");
42         System.out.println("111"+ byp);
43 
44         // 把字符轉換為 JSONObject
45         String temp = bb.toString();
46         JSONObject object = JSONObject.fromObject(temp);
47         System.out.println(object);
48         //轉換后根據Key返回值
49         System.out.println("qq==>" + object.get("QQ"));
50     }
51 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM