2013-12-11 49 views
1

我必須解析和存儲Facebook頁面的jSON數據。我的JSON數據是this。 我已經在Arraylist中使用了getter setter來存儲數據。 消氣二傳手類在android中解析和存儲嵌套的JSON數據

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

使用此代碼我能夠在數組列表,但挑戰存放在這個父元素數據如何存儲嵌套元素的數據喜歡喜歡和評論。 請建議我如何解決這個issue.Your寶貴意見將是巨大的appreciated.Thanks

+0

後JSON結構

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

一些變化 –

+0

閱讀我已經連接網址 – Dilip

+0

請參考討論和答覆在此線程http://stackoverflow.com/q問題/ 20516194/2389078我認爲這會幫助你 – DroidDev

回答

0

我解決了這個我不得不根據facebook.Added 2 ArrayList在吸氣二傳手類提供的JSON設計嵌套的getter和setter。在解析類做

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