json

 1 package com.hanqi;
 2 
 3 import org.json.JSONArray;
 4 import org.json.JSONException;
 5 import org.json.JSONObject;
 6 
 7 public class Test
 8 {
 9     public static void main(String[] args) 
10     {
11         //测试解析JSON
12         try {
13             String str="{\"name\":\"Tom\",\"age\":12,isMan:true}";
14             
15             JSONObject jo=new JSONObject(str);
16             System.out.println("对象的name属性"+jo.getString("name"));
17             System.out.println("对象的age属性"+jo.getInt("age"));
18             System.out.println("对象的name属性"+jo.getBoolean("isMan"));
19             
20             
21             
22             str="[1,2,3,4,5]";
23             
24             JSONArray ja=new JSONArray(str);
25             for(int i=0;i<ja.length();i++)
26             {
27                 System.out.println(ja.length());
28             }
29             
30             
31             
32             str="{\"account\":{\"username\":tom,\"password\":\"12345\"}}";
33             
34             jo=new JSONObject(str);
35             JSONObject account=jo.getJSONObject("account");
36             System.out.println(account.getString("username"));
37             System.out.println(account.getString("password"));
38         
39             } 
40             catch (JSONException e)
41             {
42                 e.printStackTrace();
43             }
44     }
45 }

 

posted @ 2016-08-10 21:32  明天会更好!!!!  阅读(124)  评论(0)    收藏  举报