Android: Parse the Nested JSON Array and JSON Object

人盡茶涼 提交于 2019-12-13 09:48:36

问题


I have this JSON content from a WordPress Site

"posts": [
 {

    "id": 67986,
    "type": "post",
    "title": "Launching New eBooks",
    "thumbnail_images": {
           "full": {
                  "url": "http://www.marineinsight.com/wp-content/uploads/2015/04/boiler-featured.png",
                  "width": 700,
                  "height": 500
                   },

           "medium": {
                   "url": "http://www.marineinsight.com/wp-content/uploads/2015/04/boiler-featured-300x214.png",
                   "width": 300,
                   "height": 214
                    },
          } 
  }  

I want to fetch the url from medium to display as image. After referring to some SO questions, I have made this code and tried to go into the loop. But somehow i get the entire thumbnail_images

JSONObject jsono = new JSONObject(data);
                jarray = jsono.getJSONArray("posts");

                for (int i = 0; i < jarray.length(); i++) {
                    JSONObject object = jarray.getJSONObject(i);
                    JSONArray bigImage = object.getJSONArray("thumbnail_images");
                    for (int j = 0; j < bigImage.length(); j++) {
                        JSONObject tiObj = bigImage.getJSONObject(j);
                        JSONArray tiMed = tiObj.getJSONArray("medium");
                        for (int k = 0; k < tiMed.length(); k++) {
                            JSONObject tiMedU = tiMed.getJSONObject(i);
                           String imageURL = tiMedU.getString("url");

                        }

                    }


                    actor = new Actors();

                    actor.setName(object.getString("title"));
                    actor.setDescription(object.getString("url"));
                    actor.setImage(imageURL);
                    actor.setDob(object.getString("content"));

                    actorsList.add(actor);
                }

Not able to figure out whats wrong in the loops above. Any help wld be great. Thanks


回答1:


Try to use this

 JSONObject jsono = new JSONObject(data);
            jarray = jsono.getJSONArray("posts");

            for (int i = 0; i < jarray.length(); i++) {
                 JSONObject object = jarray.getJSONObject(i);
                    JSONObject bigImage = object.getJSONObject("thumbnail_images");
                    JSONObject tiMed = bigImage.getJSONObject("medium");
                    String imageURL = tiMed.getString("url");
                    }

                }


                actor = new Actors();

                actor.setName(object.getString("title"));
                actor.setDescription(object.getString("url"));
                actor.setImage(imageURL);
                actor.setDob(object.getString("content"));

                actorsList.add(actor);
            }



回答2:


 jsonarray.getJSONObject(i).
getJSONObject("thumbnail_images").
getJSONObject("medi‌​um").getString("url")


JSONObject jsonObject = new JSONObject(result);
                JSONArray jsonArray = jsonObject.getJSONArray("posts");
                for (int i = 0; i < jsonArray.length(); i++){
                    //JSONObject jsonObject1 = jsonArray.getJSONObject(i).getJSONObject("thumbnail_images");
                    System.out.println("apk----------------"+jsonArray.getJSONObject(i).getJSONObject("thumbnail_images").getJSONObject("medium").getString("url"));
                }



回答3:


response = CustomHttpClient.executeHttpGet("http://10.0.0.4:8000/login/?format=json&name="+username.getText().toString()+"&password="+pwd.getText().toString());
      JSONArray jArray=new JSONArray(response);//Json Array
      for(int i=0;i<jArray.length();i++)
      {
      JSONObject json_data = jArray.getJSONObject(i);//Json Array To Json Object
      Jenter code hereSONObject jsonObj2 = json_data.getJSONObject("fields");
      usertype = jsonObj2.getString("usertype");
      email = jsonObj2.getString("email");
      }
      Toast.makeText(getBaseContext(),email, Toast.LENGTH_SHORT).show();   


来源:https://stackoverflow.com/questions/29798565/android-parse-the-nested-json-array-and-json-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!