国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

Android中的JSONObject和JSONArray解析json數據

2024-05-04 21:48:47
字體:
來源:轉載
供稿:網友

今天介紹一下關于json數據解析,我們使用Android中的JSONObject和JSONArray解析json數據,有android開發的朋友可以參考一下.

  1. String strJson = "{"students":[{"name":"Jack","age":12}, {"name":"Vista","age":23}, {"name":"Kaka","age":22}, {"name":"Hony","age":31}]}"; 
  2.         try { 
  3.             JSONObject jo = new JSONObject(strJson); 
  4.             JSONArray jsonArray = (JSONArray) jo.get("students"); 
  5.             for (int i = 0; i < jsonArray.length(); ++i) { 
  6.                 JSONObject o = (JSONObject) jsonArray.get(i); 
  7.                 System.out.println("name:" + o.getString("name") + "," + "age:" 
  8.                         + o.getInt("age")); 
  9.             } 
  10.         } catch (JSONException e) { 
  11.             e.printStackTrace(); 
  12.         } 
  13.  
  14.  
  15.  
  16. 2.使用gson中的JsonReader解析json數據 
  17.  
  18. try { 
  19.             String string = "{"class":1, "students":[{"name":"jack", "age":21},{"name":"kaka", "age":21},{"name":"lucy", "age":21}]}"; 
  20.             StringReader sr = new StringReader(string); 
  21.             JsonReader jr = new JsonReader(sr); 
  22.             jr.beginObject(); 
  23.             if (jr.nextName().contains("class")) { 
  24.                 System.out.println("班級: " + jr.nextString()); 
  25.                 if (jr.nextName().equals("students")) { 
  26.                     jr.beginArray(); 
  27.                     while (jr.hasNext()) { 
  28.                         jr.beginObject(); 
  29.                         if (jr.nextName().equals("name")) 
  30.                             System.out.print("姓名:" + jr.nextString()); 
  31.                         if (jr.nextName().equals("age")) { 
  32.                             System.out.println(" , 年齡:" + jr.nextInt()); 
  33.                         } 
  34.                         jr.endObject(); 
  35.                     } 
  36.                     jr.endArray(); 
  37.                 } 
  38.             } 
  39.             jr.endObject(); 
  40.         } catch (FileNotFoundException e) { 
  41.             // TODO Auto-generated catch block 
  42.             e.printStackTrace(); 
  43.         } catch (IOException e) { 
  44.             // TODO Auto-generated catch block 
  45.             e.printStackTrace(); 
  46.         } 

Json解析庫gson:http://code.google.com/p/google-gson/

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 成武县| 体育| 曲阜市| 德昌县| 绵竹市| 天峻县| 通化县| 阳春市| 临清市| 隆安县| 隆子县| 株洲县| 鄱阳县| 斗六市| 山东| 乃东县| 罗田县| 珲春市| 洪雅县| 克什克腾旗| 孟村| 三明市| 苍梧县| 周至县| 海原县| 和顺县| 吉安市| 瓮安县| 加查县| 宝丰县| 荔浦县| 铜山县| 乌鲁木齐市| 万源市| 南充市| 绥芬河市| 乌恰县| 临海市| 辽阳县| 丽江市| 乌什县|