簡體   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