繁体   English   中英

如何在android中从JSONArray中提取JSONObject

[英]How extract JSONObject from JSONArray in android

我只想从此 jsonarray 中读取“值”对象

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

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


} 

希望能帮助到你

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM