简体   繁体   English

如何在android中从JSONArray中提取JSONObject

[英]How extract JSONObject from JSONArray in android

I want to read only the "value" object from this jsonarray我只想从此 jsonarray 中读取“值”对象

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

This is my code:这是我的代码:

  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 result is the response from the rest server. JSONObject 结果是来自其余服务器的响应。

body is not array its object, you need to get und out of body as JSONArray first 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());
        }


} 

Hope it helps希望能帮助到你

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

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