2

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

shkschneider
  • 17,833
  • 13
  • 59
  • 112
Sasha
  • 644
  • 8
  • 22
  • you need to a/ get the attachement jsonarray. b/ loop on this array c/ search for the jsonobject that has `type == 'photo'` d/ get the photo jsonobject e/ get the photo_75 property you need. Alternatively, make a model out of all this, and put that in gson. – njzk2 Dec 04 '14 at 16:42
  • but how enter to attachments array – Sasha Dec 04 '14 at 16:48
  • i need to enter and get because i get no value exception – Sasha Dec 04 '14 at 16:49

2 Answers2

7

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"));
         }
aadi53
  • 439
  • 4
  • 17
  • should I delete JSONObject response1 = response.getJSONObject("response"); feedArray = parent.getJSONArray("items");* – Sasha Dec 04 '14 at 17:05
  • If you are using those statements only to get "attachments", you can delete them!! – aadi53 Dec 04 '14 at 17:07
  • and if need to get photo 75 – Sasha Dec 05 '14 at 03:01
  • thanks but sometimes i data can be only text in this case I am also get error should I check before JSONArray feedArray1 = feedObj.getJSONArray("attachments"); with if statements – Sasha Dec 05 '14 at 16:33
  • Of course, you must validate the json response before parsing it, and also handle the exceptions. – aadi53 Dec 05 '14 at 16:38
  • do you know this question?http://stackoverflow.com/questions/27363047/parse-json-date-in-java-android-in-my-case – Sasha Dec 08 '14 at 17:06
  • hello how are you?do you know how to skip ban I cant post any question know iI have edited all my questions can you help? – Sasha Dec 20 '14 at 13:39
  • hello how are you?do you know how to skip ban I cant post any question know iI have edited all my questions can you help? – Sasha Dec 20 '14 at 16:57
1

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

rock_win
  • 755
  • 1
  • 5
  • 14