1

I have to parse and store jSON data of facebook page.My JSON data is this . I have used getter setter for store data in Arraylist. Getter Setter class

public class FBStatus {
    private String postId, category, name, catId, story, picture, type,
            status_type, objectId, createdTime, updatedTime, shareCount;

    public String getCategory() {
        return category;
    }

    public String getPostId() {
        return postId;
    }

    public void setPostId(String postId) {
        this.postId = postId;
    }

    public String getCatId() {
        return catId;
    }

    public void setCatId(String catId) {
        this.catId = catId;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getStory() {
        return story;
    }

    public void setStory(String story) {
        this.story = story;
    }

    public String getPicture() {
        return picture;
    }

    public void setPicture(String picture) {
        this.picture = picture;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getStatus_type() {
        return status_type;
    }

    public void setStatus_type(String status_type) {
        this.status_type = status_type;
    }

    public String getObjectId() {
        return objectId;
    }

    public void setObjectId(String objectId) {
        this.objectId = objectId;
    }

    public String getCreatedTime() {
        return createdTime;
    }

    public void setCreatedTime(String createdTime) {
        this.createdTime = createdTime;
    }

    public String getUpdatedTime() {
        return updatedTime;
    }

    public void setUpdatedTime(String updatedTime) {
        this.updatedTime = updatedTime;
    }

    public String getShareCount() {
        return shareCount;
    }

    public void setShareCount(String shareCount) {
        this.shareCount = shareCount;
    }

    public class Likes {
        private String id, name;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

    }

    class Comment {
        private String id, name, commenterId, message, createdTime, likecount;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getCommenterId() {
            return commenterId;
        }

        public void setCommenterId(String commenterId) {
            this.commenterId = commenterId;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public String getCreatedTime() {
            return createdTime;
        }

        public void setCreatedTime(String createdTime) {
            this.createdTime = createdTime;
        }

        public String getLikecount() {
            return likecount;
        }

        public void setLikecount(String likecount) {
            this.likecount = likecount;
        }

    }
}

Parsing code

private ArrayList<FBStatus> mData = new ArrayList<FBStatus>();
JsonParsing parsing = new JsonParsing();
        jObj = parsing.getJSONFromUrl(FB_COMMENT + mAccessToken);
        try {
            data = jObj.getJSONArray(TAG_DATA);
            for (int i = 0; i < data.length(); i++) {
                FBStatus bean = new FBStatus();
                JSONObject c = data.getJSONObject(i);
                bean.setPostId(c.getString(TAG_ID));
                JSONObject fromElement = new JSONObject(data.getJSONObject(i)
                        .getString(TAG_FROM));
                if (fromElement != null) {
                    bean.setCategory(fromElement.getString(TAG_CATEGORY));
                    bean.setCatId(fromElement.getString(TAG_ID));
                    bean.setName(fromElement.getString(TAG_NAME));
                }
                JSONObject shareElement = new JSONObject(data.getJSONObject(i)
                        .getString(TAG_SHARE));
                if (shareElement != null) {
                    bean.setShareCount(shareElement.getString(TAG_SHARE_COUNT));
                }
                JSONObject likesObj = new JSONObject(data.getJSONObject(i)
                        .getString(TAG_LIKES));
                JSONArray likesArray = likesObj.getJSONArray(TAG_DATA);
                for (int index = 0; index < likesArray.length(); index++) {
                    JSONObject likesElement = likesArray.getJSONObject(index);
                    Likes like = bean.new Likes();
                    like.setId(likesElement.getString(TAG_ID));
                    like.setName(likesElement.getString(TAG_NAME));
                    // mData.add();
                }
                JSONObject commentObj = new JSONObject(data.getJSONObject(i)
                        .getString(TAG_COMMENT));
                JSONArray commentArray = commentObj.getJSONArray(TAG_DATA);
                for (int index = 0; index < commentArray.length(); index++) {
                    JSONObject commentElement = commentArray
                            .getJSONObject(index);
                    String commentId = commentElement.getString(TAG_ID);
                    String commentMsg = commentElement.getString(TAG_MESSAGE);
                    String commentCreatedTime = commentElement
                            .getString(TAG_CREATED_TIME);
                    String commentLikeCount = commentElement
                            .getString(TAG_LIKE_COUNT);
                    JSONObject fromCommentElement = new JSONObject(commentArray
                            .getJSONObject(index).getString(TAG_FROM));
                    if (fromCommentElement != null) {
                        String commentName = fromCommentElement
                                .getString(TAG_NAME);
                        String commenterId = fromCommentElement
                                .getString(TAG_ID);
                    }
                }
                bean.setStory(c.getString(TAG_STORY));
                bean.setPicture(c.getString(TAG_PICTURE));
                bean.setType(c.getString(TAG_TYPE));
                bean.setStatus_type(c.getString(TAG_STATUS_TYPE));
                bean.setObjectId(c.getString(TAG_OBJECT_ID));
                bean.setCreatedTime(c.getString(TAG_CREATED_TIME));
                bean.setUpdatedTime(c.getString(TAG_UPDATED_TIME));
                mData.add(bean);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

Using this code I'm able to store parent elements data in arraylist but challenge in this how to to store data of nested element like likes and comments. Please suggest me how to solve this issue.Your valuable suggestion will be great appreciated.Thanks

Dilip
  • 2,172
  • 5
  • 29
  • 50

1 Answers1

0

I Solved this I had to design nested getter and setter according to the JSON provided by facebook.Added two ArrayList in getter setter class.

  public List<Likes> likesData = new ArrayList<FBStatus.Likes>();
    public List<Comment> commentData = new ArrayList<FBStatus.Comment>();

Some changes done in parsing class

    private ArrayList<FBStatus> mData = new ArrayList<FBStatus>();
JsonParsing parsing = new JsonParsing();
        jObj = parsing.getJSONFromUrl(FB_COMMENT + mAccessToken);
        try {
            data = jObj.getJSONArray(TAG_DATA);
            for (int i = 0; i < data.length(); i++) {
                FBStatus bean = new FBStatus();
                JSONObject c = data.getJSONObject(i);
                bean.setPostId(c.getString(TAG_ID));
                JSONObject fromElement = new JSONObject(data.getJSONObject(i)
                        .getString(TAG_FROM));
                if (fromElement != null) {
                    bean.setCategory(fromElement.getString(TAG_CATEGORY));
                    bean.setCatId(fromElement.getString(TAG_ID));
                    bean.setName(fromElement.getString(TAG_NAME));
                }
                JSONObject shareElement = new JSONObject(data.getJSONObject(i)
                        .getString(TAG_SHARE));
                if (shareElement != null) {
                    bean.setShareCount(shareElement.getString(TAG_SHARE_COUNT));
                }
                JSONObject likesObj = new JSONObject(data.getJSONObject(i)
                        .getString(TAG_LIKES));
                JSONArray likesArray = likesObj.getJSONArray(TAG_DATA);
                for (int index = 0; index < likesArray.length(); index++) {
                    JSONObject likesElement = likesArray.getJSONObject(index);
                    Likes like = bean.new Likes();
                    like.setId(likesElement.getString(TAG_ID));
                    like.setName(likesElement.getString(TAG_NAME));
                    bean.likesData.add(like);
                }
                JSONObject commentObj = new JSONObject(data.getJSONObject(i)
                        .getString(TAG_COMMENT));
                JSONArray commentArray = commentObj.getJSONArray(TAG_DATA);
                for (int index = 0; index < commentArray.length(); index++) {
                    JSONObject commentElement = commentArray
                            .getJSONObject(index);
                    Comment comment = bean.new Comment();
                    comment.setId(commentElement.getString(TAG_ID));
                    comment.setMessage(commentElement.getString(TAG_MESSAGE));
                    comment.setCreatedTime(commentElement
                            .getString(TAG_CREATED_TIME));
                    comment.setLikecount(commentElement
                            .getString(TAG_LIKE_COUNT));
                    JSONObject fromCommentElement = new JSONObject(commentArray
                            .getJSONObject(index).getString(TAG_FROM));
                    if (fromCommentElement != null) {
                        comment.setName(fromCommentElement.getString(TAG_NAME));
                        comment.setCommenterId(fromCommentElement
                                .getString(TAG_ID));
                    }
                    bean.commentData.add(comment);
                }
                bean.setStory(c.getString(TAG_STORY));
                bean.setPicture(c.getString(TAG_PICTURE));
                bean.setType(c.getString(TAG_TYPE));
                bean.setStatus_type(c.getString(TAG_STATUS_TYPE));
                bean.setObjectId(c.getString(TAG_OBJECT_ID));
                bean.setCreatedTime(c.getString(TAG_CREATED_TIME));
                bean.setUpdatedTime(c.getString(TAG_UPDATED_TIME));
                mData.add(bean);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
Dilip
  • 2,172
  • 5
  • 29
  • 50