android 读取json数据(遍历JSONObject和JSONArray)

android 读取json数据(遍历JSONObject和JSONArray)

 
 
  1. public String getJson(){  
  2.         String jsonString = "{\"FLAG\":\"flag\",\"MESSAGE\":\"SUCCESS\",\"name\":[{\"name\":\"jack\"},{\"name\":\"lucy\"}]}";//json字符串  
  3.         try {  
  4.             JSONObject result = new JSONObject(jsonstring);//转换为JSONObject  
  5.             int num = result.length();  
  6.             JSONArray nameList = result.getJSONArray("name");//获取JSONArray  
  7.             int length = nameList.length();  
  8.             String aa = "";  
  9.             for(int i = 0; i < length; i++){//遍历JSONArray  
  10.                 Log.d("debugTest",Integer.toString(i));  
  11.                 JSONObject oj = nameList.getJSONObject(i);  
  12.                 aa = aa + oj.getString("name")+"|";  
  13.                   
  14.             }  
  15.             Iterator<?> it = result.keys();  
  16.             String aa2 = "";  
  17.             String bb2 = null;  
  18.             while(it.hasNext()){//遍历JSONObject  
  19.                 bb2 = (String) it.next().toString();  
  20.                 aa2 = aa2 + result.getString(bb2);  
  21.                   
  22.             }  
  23.             return aa;  
  24.         } catch (JSONException e) {  
  25.             throw new RuntimeException(e);  
  26.         }  
  27.     }  
  28.    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值