Sasha
Sasha

Reputation: 644

get data from Json array in Json array Android Java

Hello how to get data from Json Array in another Json array i have get data till attachments but attachment doesnt work, All code work till attachments how to get data from attachments I need to get "photo_75" from it

Json

"response":{  
  "count":3,
  "items":[  
     {  
        "id":3,
        "from_id":205110032,
        "owner_id":-81865402,
        "date":1417672154,
        "post_type":"post",
        "text":"jjjjASDFGHJKYTRDXCVB",
        "attachments":[  
           {  
              "type":"photo",
              "photo":{  
                 "id":330414711,
                 "album_id":-7,
                 "owner_id":205110032,
                 "photo_75":"http:\/\/cs605116.vk.me\/v605116032\/6325\/3SwTo8j4lJ0.jpg",
                 "photo_130":"http:\/\/cs605116.vk.me\/v605116032\/6326\/_OZA86FO3Nw.jpg",
                 "photo_604":"http:\/\/cs605116.vk.me\/v605116032\/6327\/AUtB59708Nw.jpg",
                 "photo_807":"http:\/\/cs605116.vk.me\/v605116032\/6328\/59oAdfz9jgI.jpg",
                 "width":538,
                 "height":807,
                 "text":"",
                 "date":1399134687,
                 "access_key":"7297eb663de2e4e6b2"
              }
           }
        ],
        "comments":{  
           "count":0
        },
        "likes":{  
           "count":0
        },
        "reposts":{  
           "count":0
        }
     },

Java

private void parseJsonFeed(JSONObject response) {
    try {
        JSONObject parent =  response.getJSONObject("response");

        JSONArray feedArray = parent.getJSONArray("items");

        for (int i = 0; i < feedArray.length(); i++) {
            JSONObject feedObj = (JSONObject) feedArray.get(i);

            FeedItem item = new FeedItem();
            item.setId(feedObj.getInt("id"));           


            item.setName(feedObj.getString("post_type"));
            item.setTimeStamp(feedObj.getString("date"));


            // Image might be null sometimes
            String image = feedObj.isNull("photo") ? null : feedObj
                    .getString("photo");
            item.setImge(image);
            item.setStatus(feedObj.getString("text"));

        All code work till there how to get data from attachments
             ***JSONObject response1 = response.getJSONObject("response");
             feedArray = parent.getJSONArray("items");***

            JSONArray feedArray1 = response1.getJSONArray("attachments");

            for (int i1 = 0; i1 < feedArray1.length(); i1++) {
                 JSONObject  feedObj1 = (JSONObject) feedArray1.get(i1);

                 FeedItem item1 = new FeedItem();

                item.setProfilePic(feedObj1.getString("photo_75"));


             }



            // url might be null sometimes
            String feedUrl = feedObj.isNull("url") ? null : feedObj
                    .getString("url");
            item.setUrl(feedUrl);

            feedItems.add(item);
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }
}

Thanks in advance

Upvotes: 2

Views: 8096

Answers (2)

aadi53
aadi53

Reputation: 449

you are looking for attachments in wrong object. "attachmetnts" is property of item. instead of

JSONArray feedArray1 = response1.getJSONArray("attachments");

use

JSONArray feedArray1 = feedObj.getJSONArray("attachments");

in your case feedObj contains item object.

to get photo : Remove lines :

        String image = feedObj.isNull("photo") ? null : feedObj
                .getString("photo");
        item.setImge(image);

and change it to :

    for (int i1 = 0; i1 < feedArray1.length(); i1++) {
            JSONObject  attachment = (JSONObject) feedArray1.get(i1);
            JSONObject photo = (JSONObject) attachment.getJSONObject("photo");
            item.setImge(photo);
            item.setProfilePic(photo.getString("photo_75"));
            item.setStatus(photo.getString("text"));
         }

Upvotes: 7

rock_win
rock_win

Reputation: 755

You can try GSON, which would directly give you a java object from your json and you would not have to parse it manually.

Upvotes: 1

Related Questions