在android中解析和存储嵌套的JSON数据

在android中解析和存储嵌套的JSON数据,android,json,facebook,Android,Json,Facebook,我必须解析和存储facebook页面的jSON数据。我的jSON数据是。 我使用getter setter在Arraylist中存储数据。 Getter Setter类 public class FBStatus { private String postId, category, name, catId, story, picture, type, status_type, objectId, createdTime, updatedTime, shareCou

我必须解析和存储facebook页面的jSON数据。我的jSON数据是。 我使用getter setter在Arraylist中存储数据。 Getter Setter类

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;
        }

    }
}
解析代码

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();
        }
private ArrayList mData=new ArrayList();
JsonParsing=新的JsonParsing();
jObj=parsing.getJSONFromUrl(FB_COMMENT+mAccessToken);
试一试{
data=jObj.getJSONArray(TAG_数据);
对于(int i=0;i
使用这段代码,我能够将父元素数据存储在arraylist中,但在这方面我遇到了一个难题,即如何存储嵌套元素的数据,如喜欢和注释。
请建议我如何解决这个问题。非常感谢您的宝贵建议。谢谢

我解决了这个问题,我不得不根据facebook提供的JSON设计嵌套的getter和setter。在getter setter类中添加了两个
ArrayList

  public List<Likes> likesData = new ArrayList<FBStatus.Likes>();
    public List<Comment> commentData = new ArrayList<FBStatus.Comment>();
公共列表likesData=newarraylist();
public List commentData=new ArrayList();
解析类中所做的一些更改

    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();
        }
private ArrayList mData=new ArrayList();
JsonParsing=新的JsonParsing();
jObj=parsing.getJSONFromUrl(FB_COMMENT+mAccessToken);
试一试{
data=jObj.getJSONArray(TAG_数据);
对于(int i=0;i