android 讀取json數據(遍歷JSONObject和JSONArray)(轉)


  • public String getJson(){  
  •         String jsonString = "{\"FLAG\":\"flag\",\"MESSAGE\":\"SUCCESS\",\"name\":[{\"name\":\"jack\"},{\"name\":\"lucy\"}]}";//json字符串  
  •         try {  
  •             JSONObject result = new JSONObject(jsonstring);//轉換為JSONObject  
  •             int num = result.length();  
  •             JSONArray nameList = result.getJSONArray("name");//獲取JSONArray  
  •             int length = nameList.length();  
  •             String aa = "";  
  •             for(int i = 0; i < length; i++){//遍歷JSONArray  
  •                 Log.d("debugTest",Integer.toString(i));  
  •                 JSONObject oj = nameList.getJSONObject(i);  
  •                 aa = aa + oj.getString("name")+"|";  
  •                   
  •             }  
  •             Iterator<?> it = result.keys();  
  •             String aa2 = "";  
  •             String bb2 = null;  
  •             while(it.hasNext()){//遍歷JSONObject  
  •                 bb2 = (String) it.next().toString();  
  •                 aa2 = aa2 + result.getString(bb2);  
  •                   
  •             }  
  •             return aa;  
  •         } catch (JSONException e) {  
  •             throw new RuntimeException(e);  
  •         }  
  •     }  
 
        

 

原文: http://zhaojianping.blog.51cto.com/725123/629526


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM