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 }

 

posted @ 2019-08-07 20:41  bai白  阅读(5325)  评论(0编辑  收藏  举报