如何从android中的JSONArray中提取JSONObject

时间:2015-09-04 16:04:38

标签: java android json

我想从这个jsonarray中只读取“value”对象

"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结果是来自其他服务器的响应。

1 个答案:

答案 0 :(得分:0)

body不是它的对象数组,你需要首先从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());
        }


} 

希望有所帮助