Android之json数据的解析方法

来源:互联网 发布:什么是淘宝团队 编辑:程序博客网 时间:2024/05/18 02:12

转载请注明出处:http://blog.csdn.net/u010214991/article/details/48393815

本篇跟大家介绍的是Android Json数据的几种解析方法,大家都知道,我们在向服务器发送请求获取JSON字符串之后常常要进行信息的提取,这实际上就是解析的过程。解析json数据有很多种方法,如果你打算将json数据解析并封装到一个类中,那么我建议你使用Google提供的解析工具包,调用fromJson将json数据解析成对象,或者toJson将一个对象解析为一个字符串,使用起来简单暴力,非常方便。不过本章主要讲的是以下几种基本的解析情况:


1、简单的json字符串,不包含数组

 String json = "{\"username\":\"huoying\",\"age\":38,\"address\":\"广东省东莞市茶山镇\"}"; try { JSONObject jsonObject = new JSONObject(json); Toast.makeText( context, jsonObject.getString("username") + jsonObject.getString("age") + jsonObject.getString("address"), Toast.LENGTH_SHORT) .show(); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); }


2、 json数据中含有数组

 String json = "{\"username\":\"张木生\",age:11,\"jicheng\":[{\"zhengshu\":\"PMP\",\"date\":\"2011年\"},{\"zhengshu\":\"信息系统项目管理师\",\"date\":\"2012年\"}],\"addr\":\"江西\"}"; String str = ""; try { JSONObject jsonObject = new JSONObject(json); String username = jsonObject.getString("username"); int age = jsonObject.getInt("age"); JSONArray jsonArray = jsonObject.getJSONArray("jicheng"); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject2 = (JSONObject) jsonArray.opt(i); str += jsonObject2.getString("zhengshu")+jsonObject2.getString("date"); } Toast.makeText(this, str, Toast.LENGTH_SHORT).show(); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); }

3、json数据中只有数组

String json = "[{\"name\":\"张三\",\"age\":\"20\",\"sex\":\"男\"},{\"name\":\"凤姐\",\"age\":\"18\",\"sex\":\"女\"}]";         String str="";try {JSONArray jsonArray = new JSONArray(json);for (int i = 0; i < jsonArray.length(); i++) {JSONObject jsonObject = (JSONObject) jsonArray.opt(i); str += jsonObject.getString("name")+jsonObject.getString("age")+jsonObject.getString("sex");}Toast.makeText(context, str, Toast.LENGTH_SHORT).show();} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();}

4、数组里面又有数组,即json数据的数组里面嵌套数组

   String json="["+               "{\"devid\":\"1234567800\",\"gps\":[{\"time\":\"2014-11-12\",\"latitude\":\"29.4963\",\"longitude\":\"116.189\"},{\"time\":\"2014-11-12\",\"latitude\":\"29.4963\",\"longitude\":\"116.189\" }],\"devname\":\"赣01\"},"+               " {\"devid\":\"1234567800\",\"gps\":[{\"time\":\"2014-11-12\",\"latitude\":\"29.4963\",\"longitude\":\"116.189\"},{\"time\":\"2014-11-12\",\"latitude\":\"29.4963\",\"longitude\":\"116.189\" }],\"devname\":\"赣92\"},"+               " {\"devid\":\"1234567800\",\"gps\":[{\"time\":\"2014-11-12\",\"latitude\":\"29.4963\",\"longitude\":\"116.189\"},{\"time\":\"2014-11-12\",\"latitude\":\"29.4963\",\"longitude\":\"116.189\" }],\"devname\":\"赣43\"}"+               "]";   String time="";   String latitude="";   try {JSONArray jsonArray = new JSONArray(json);for (int i = 0; i < jsonArray.length(); i++) {JSONObject jsonObject = (JSONObject) jsonArray.opt(i);String devid = jsonObject.getString("devid");JSONArray jsonArray2 = jsonObject.getJSONArray("gps");for (int j = 0; j < jsonArray2.length(); j++) {JSONObject jsonObject2 = (JSONObject) jsonArray2.opt(j);time += jsonObject2.getString("time");latitude += jsonObject2.getString("latitude");}}} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();}

内容比较基础,希望能给初学者提供帮助,想要了解如何使用Google提供的json解析工具包解析数据,请关注此篇文章《基于Google Json工具包解析数据》


1 0