Android解析Json示范

 1 public class OneJsonActivity extends Activity {
 2 
 3     String JsonData = "[{\"id\":[\"386\",\"381\",\"379\",\"377\"],\"num\":[\"386\",\"381\",\"379\",\"377\"]},{\"id\":[\"3860\",\"3810\",\"3790\",\"3770\"],\"num\":[\"3860\",\"3810\",\"3790\",\"3770\"]}]";
 4     JSONObject obj = null;
 5     JSONArray jsonArary;
 6 
 7     @Override
 8     protected void onCreate(Bundle savedInstanceState) {
 9         super.onCreate(savedInstanceState);
10         setContentView(R.layout.one_json_activity);
11         
12 
13         try {
14             jsonArary = new JSONArray(JsonData);
15             for (int i = 0; i < jsonArary.length(); i++) {
16                 obj = jsonArary.getJSONObject(i);
17                 // 取ID
18                 JSONArray strID = obj.getJSONArray("id");
19                 for (int j = 0; j < strID.length(); j++) {
20                     Log.v("ID:", strID.get(j).toString());
21                 }
22                 // 取num
23                 JSONArray strNum = obj.getJSONArray("num");
24                 for (int k = 0; k < strNum.length(); k++) {
25                     Log.v("NUM:", strNum.get(k).toString());
26                 }
27                 
28             }
29         } catch (JSONException e) {
30             e.printStackTrace();
31         }
32 
33     }
34 
35 }

JSON在线校验、格式化、视图( {} 对象 [] 数组 )

http://www.bejson.com/jsonviewernew/

 

posted @ 2015-04-14 09:42  小菜希  阅读(287)  评论(0编辑  收藏  举报