2016-04-02 7 views
0

Ich hava dieses JSON einige Elemente in verschachtelten JSON-Objekten erhalten:Wie in einem JSON-Array

[ 
    { 

    "title": "This a Sample title for each post_title", 

    "excerpt": "And this is a sample of the post_body, 

    "author": "King Spark", 

    "featured_picture": { 

     "source": "https://exapmple.com/blah/blah/image.jpg", 
     "year": "2015", 
     "ownwer": "Akim Man", 

    }, 

    },... 

Vom json Ich brauche nur den Titel, Auszug Elemente der wichtigsten Objekte. Dann von featured_picture Objekte, ich möchte nur die Quelle Element.

Ich habe diesen Code geschrieben, und es scheint nicht zu funktionieren:

private void parseData(JSONArray array){ 
     Log.d(TAG, "Parsing array"); 

     for(int i = 0; i<array.length(); i++) { 
      PostItems postItem = new PostItems(); 
      JSONObject jsonObject = null; 
      try { 
       jsonObject = array.getJSONObject(i); 
       postItem.setPost_title(jsonObject.getString(ConfigPost.TAG_POST_TITLE)); 
       postItem.setPost_body(jsonObject.getString(ConfigPost.TAG_POST_BODY)); 

       //Parsing featured_pocture object 


       for (int f = 0; f<array.length(); f++) { 
        JSONObject object = array.getJSONObject(f); 
        JSONObject postImage = object.getJSONObject("featured_picture"); 
        String imageURL = postImage.getString("source"); 
        postItem.setPost_image(imageURL); 
       } 





      } catch (JSONException w) { 
       w.printStackTrace(); 
       //Toast.makeText(this, "Error in parsing Json", Toast.LENGTH_LONG).show(); 
      } 
      mPostItemsList.add(postItem); 
     } 

    } 

Antwort

0

Versuchen Sie, die verschachtelten JSON wie auf diese Weise zu analysieren:

private void parseData(JSONArray array){ 
    Log.d(TAG, "Parsing array"); 

    for(int i = 0; i<array.length(); i++) { 
     PostItems postItem = new PostItems(); 
     JSONObject jsonObject = null; 
     try { 
      jsonObject = array.getJSONObject(i); 

    postItem.setPost_title(jsonObject.getString(ConfigPost.TAG_POST_TITLE)); 

    postItem.setPost_body(jsonObject.getString(ConfigPost.TAG_POST_BODY)); 

    //Parsing featured_picture object 
    JSONObject postImage = jsonObject.getJSONObject("featured_picture"); 

    postItem.setPost_image(postImage.getString("source")); 


     } catch (JSONException w) { 
      w.printStackTrace(); 
      //Toast.makeText(this, "Error in parsing Json", Toast.LENGTH_LONG).show(); 
     } 
     mPostItemsList.add(postItem); 
    } 

} 
+0

Danke, es hat funktioniert! :) – Faraday

0

Sie werden weiterhin nicht das Array lesen Sie hier

for (int f = 0; f<array.length(); f++) { 

featured_picture einen Eintrag in der Karte und gibt eine Karte auch.

Der Zugang sollte wie folgt sein:

array.getJSONObject(i).getJSONObject("featured_picture").getString("source"); 
0

Sie haben Objekt und Array in json zu identifizieren, dann wird der Wert von Schlüssel finden, wenn Sie dann Komplexität von json keine Rolle gelernt follow tutorial

0
zu analysieren

Ihr Code for (int f = 0; f<array.length(); f++) { JSONObject object = array.getJSONObject(f); JSONObject postImage = object.getJSONObject("featured_picture"); String imageURL = postImage.getString("source"); postItem.setPost_image(imageURL); } ist nicht korrekt, dieser Teil von JSON ist kein Array, sondern ein Objekt innerhalb eines anderen JsonObject.

0

hier keine Notwendigkeit Schleife zu durchlaufen verschachtelte JSONobject zu lesen.

Weil "featured_picture" gibt nur JSONObject kein Array. Im Fall, wenn seine Rückkehr Array sollten Sie wie folgt zu lesen:

JSONObject rootObject=new JSONObject(); 
JSONArray nestedObject=rootObject.getJSONArray("key"); 

Hier geändert i Code in der richtigen Art und Weise Hoffnung wird es Ihnen helfen.

for(int i = 0; i<array.length(); i++) { 
      PostItems postItem = new PostItems(); 
      JSONObject jsonObject = null; 
      try { 
       jsonObject = array.getJSONObject(i); 
        postItem.setPost_title(jsonObject.getString(ConfigPost.TAG_POST_TITLE)); 
       postItem.setPost_body(jsonObject.getString(ConfigPost.TAG_POST_BODY)); 

       //Parsing featured_pocture object 

JSONObject postImage = jsonObject.getJSONObject("featured_picture"); 
    String imageURL = postImage.getString("source"); 
     postItem.setPost_image(imageURL); 


      } catch (JSONException w) { 
       w.printStackTrace(); 
       //Toast.makeText(this, "Error in parsing Json", Toast.LENGTH_LONG).show(); 
      }