2015-09-04 63 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