2015-09-04 67 views
-2

提取的JSONObject我想從這個jsonarray只讀「價值」對象如何從JSONArray android的

"body":{"und":[{"value":"hi friends!!!","summary":null,"format":null}]} 

這是我的代碼:

protected void onPostExecute(final JSONObject result) { 



      try { 
       TextView lst2 = (TextView) findViewById(R.id.View2); 


       JSONArray cast = result.getJSONArray("body"); 
       for (int i=0; i<cast.length(); i++) { 
        JSONObject actor = cast.getJSONObject(i); 
        String name = actor.getString("value"); 
        lst2.setText(name); 
       } 




      } catch (Exception e2) { 
       Log.v("Error adding article", e2.getMessage()); 
      } 


    } 

JSONObject的結果是從休息響應服務器。

+0

見這個例子中,如果有幫助:http://stackoverflow.com/問題/ 32092668/acess-nested-object-of-json-in-java/32093501#32093501 –

+0

你的代碼中有什麼/沒有發生?此外,將文本設置爲像這樣的循環只會將其設置爲最後一個值(即無論在'cast.getJSONObject(cast.length()-1)' – codeMagic

+0

「body」:{「und」:[{「價值「:」嗨朋友!!!「,」摘要「:null,」format「:null}]}這是無效的jason。 –

回答

0

身體是不是它的數組對象,你需要得到UND排出體外的JSONArray第一

protected void onPostExecute(final JSONObject result) { 



     try { 
      TextView lst2 = (TextView) findViewById(R.id.View2); 


      JSONArray cast = result.getJSONObject("body").getJSONArray("und"); 
      for (int i=0; i<cast.length(); i++) { 
       JSONObject actor = cast.getJSONObject(i); 
       String name = actor.getString("value"); 
       lst2.setText(name); 
      } 




     } catch (Exception e2) { 
      Log.v("Error adding article", e2.getMessage()); 
     } 


} 

希望它可以幫助

+0

謝謝你的工作! – Corax