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

3
  • 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. Commented Dec 4, 2014 at 16:42
  • but how enter to attachments array Commented Dec 4, 2014 at 16:48
  • i need to enter and get because i get no value exception Commented Dec 4, 2014 at 16:49

2 Answers 2

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"));
         }
Sign up to request clarification or add additional context in comments.

8 Comments

should I delete JSONObject response1 = response.getJSONObject("response"); feedArray = parent.getJSONArray("items");*
If you are using those statements only to get "attachments", you can delete them!!
and if need to get photo 75
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
Of course, you must validate the json response before parsing it, and also handle the exceptions.
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.