解析并存储在Android的嵌套JSON数据 [英] Parse and store nested JSON data in android

查看:181
本文介绍了解析并存储在Android的嵌套JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要解析和Facebook page.My JSON数据存储JSON数据是<一个href=\"https://graph.facebook.com/344128252278047/feed?access_token=CAAUJS8OQ520BAB1VlcMib0nZBRVCZCkQQhd2feiF5nkG0a2mZAtZAz6lFStkz21UJWO14XXwzEaZCxJgRl5FZCZCbdRZCjRBGBY1w6Rv2Fxy5CX6xiTQVMizE6Y6u5C2zJPtXZBXCZCo9Pc8GvlXQJ6xBD8XQUQ4SGULgYgeHJRgLDir6QuY1Q4G180SNI0wcb7l4JMePMkI1B0LPYvBgW0rFI62F4Fg4CTVVGDIqKUNMLlAZDZD\"相对=nofollow>这个。
我已经使用的getter setter方法​​的ArrayList中存储数据。
消气二传手类

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

    }
}

解析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();
        }

使用这个code我能够在ArrayList的,但挑战存储在此父元素的数据如何存储喜欢的喜好和评论嵌套元素的数据。
请建议我如何解决这个issue.Your宝贵意见将是巨大的AP preciated.Thanks

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

推荐答案

我解决了这个我要设计根据facebook.Added提供的两个JSON的嵌套的getter和setter 的ArrayList 中的getter setter方法​​类。

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>();

在解析类做了一些修改

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

这篇关于解析并存储在Android的嵌套JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆