移动开发经常要与服务器数据交互,也常使用json数据格式,那就说说Android json解析。

1.最简单json格式解析如下:

                           //解析json       try {          JSONTokener jsonParser = new JSONTokener(strResult);        JSONObject jsonObj = (JSONObject) jsonParser.nextValue();        String strsportsTitle = jsonObj.getString("sportsTitle");              int nid= jsonObj.getInt("id");                      } catch (JSONException e) {                System.out.println("Json parse error");                e.printStackTrace();           } 

字符串strResult就是需要解析json数据了。用过json数据格式都知道,json数据格式是一个键对应一个值。你可以先打印出原始数据strResult,就知道jsonObj.getString("sportsTitle");这双引号里面键是什么。

2.数组形式json数据解析如下:

try {JSONArray jsonArray = new JSONArray(strResult);for (int i = 0; i < jsonArray.length(); i++) {JSONObject jsonObj = jsonArray.optJSONObject(i);id[i] = jsonObj.getInt("id");time[i] = jsonObj.getString("time");users[i] = jsonObj.getString("users");roomTitle[i] = jsonObj.getString("roomTitle");}} catch (JSONException e) {System.out.println("Jsons parse error !");e.printStackTrace();}


3.json里面嵌套json数据解析如下:

                               try {JSONArray jsonArray = new JSONArray(strResult);for (int i = 0; i < jsonArray.length(); i++) {JSONObject jsonObj = jsonArray.optJSONObject(i);String strachievement = jsonObj.getString("achievement");String strmember = jsonObj.getString("member");try {JSONTokener jsonParser1 = new JSONTokener(achievement);JSONObject jsonObj1 = (JSONObject) jsonParser1.nextValue();nametype[i] = jsonObj1.getString("name");type[i] = jsonObj1.getString("type");} catch (JSONException e) {System.out.println("Json parse error");e.printStackTrace();}}} catch (JSONException e) {System.out.println("Json parse error");e.printStackTrace();}

嵌套json数据,其实都是一样的。多解析一次而已。

更多相关文章

  1. android ScrollView嵌套RecyclerView
  2. Android studio 解析JSON格式数据
  3. 单元测试,数据存储
  4. android Thread 数据加载 ProgressDialog 请求超时处理 线程 Han
  5. 2014-7-22 Android SharedPreferences 写入与读取数据
  6. Android开发入门之数据库例子

随机推荐

  1. Android编译错误:You have tried to chang
  2. android 开发中中,经常用到的代码
  3. Android AndBox 安安卜1.3发布
  4. Android的安全性和权限(Security and Perm
  5. android 获取Gps信息的程序源码
  6. Android 使用 TableLayout 布局拉伸宽度
  7. Apple 再挑起戰火,控告 Samsung 15款 Andr
  8. Android撸一个转盘抽奖
  9. android 中隐藏EditText的下划线方法
  10. android:configChanges属性