Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
HowToKB/src/kb/howtokb/usecase/YouTubeObj.java
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
157 lines (132 sloc)
3.67 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package kb.howtokb.usecase; | |
import java.util.ArrayList; | |
import java.util.Iterator; | |
import java.util.List; | |
import org.json.simple.JSONArray; | |
import org.json.simple.JSONObject; | |
public class YouTubeObj { | |
private String id; | |
private String title; | |
private String description; | |
private String thumbnails; | |
private List<String> tags; | |
private List<Comment> comments; | |
private String cateID; | |
public YouTubeObj(String title, String description, String thumbnails, List<String> tags, | |
String cateID) { | |
super(); | |
this.title = title; | |
this.description = description; | |
this.thumbnails = thumbnails; | |
this.tags = tags; | |
this.cateID = cateID; | |
} | |
public YouTubeObj(String id, String title, String description, String thumbnails, List<String> tags, | |
List<Comment> comments, String cateID) { | |
super(); | |
this.id = id; | |
this.title = title; | |
this.description = description; | |
this.thumbnails = thumbnails; | |
this.tags = tags; | |
this.comments = comments; | |
this.cateID = cateID; | |
} | |
public void setId(String id) { | |
this.id = id; | |
} | |
public YouTubeObj() { | |
super(); | |
// TODO Auto-generated constructor stub | |
} | |
public void setComments(List<Comment> comments) { | |
this.comments = comments; | |
} | |
public YouTubeObj(String title, String description, String thumbnails, String cateID) { | |
super(); | |
this.title = title; | |
this.description = description; | |
this.thumbnails = thumbnails; | |
this.cateID = cateID; | |
} | |
@Override | |
public String toString() { | |
return "YouTubeObj\ntitle=" + title + "\ndescription=" + description + "\nthumbnails=" + thumbnails + "\ntags=" | |
+ tags + "\ncomments=" + comments + "\ncateID=" + cateID + "\n"; | |
} | |
@SuppressWarnings("unchecked") | |
public JSONArray commentToJson(){ | |
JSONArray comment = new JSONArray(); | |
if (getComments().size() > 0){ | |
for (int i=0; i<getComments().size(); i++){ | |
comment.add(getComments().get(i).toJson()); | |
} | |
} | |
return comment; | |
} | |
@SuppressWarnings("unchecked") | |
public JSONArray tagToJson(){ | |
JSONArray tags = new JSONArray(); | |
if (getTags().size() > 0){ | |
for (int i=0; i<getTags().size(); i++){ | |
tags.add(getTags().get(i)); | |
} | |
} | |
return tags; | |
} | |
@SuppressWarnings("unchecked") | |
public JSONObject toJson(){ | |
JSONObject obj = new JSONObject(); | |
obj.put("id", getId()); | |
obj.put("title", getTitle()); | |
obj.put("description", getDescription()); | |
obj.put("thumbnail", getThumbnails()); | |
obj.put("tags", tagToJson()); | |
obj.put("cateID", getCateID()); | |
obj.put("comment", commentToJson()); | |
return obj; | |
} | |
@SuppressWarnings("unchecked") | |
public static YouTubeObj toYouTubeObj(JSONObject obj){ | |
String id = (String) obj.get("id"); | |
String title = (String) obj.get("title"); | |
String des = (String) obj.get("description"); | |
String thumbnail = (String) obj.get("thumbnail"); | |
String cateID = (String) obj.get("cateID"); | |
ArrayList<String> tags = new ArrayList<String>(); | |
JSONArray tag = (JSONArray) obj.get("tags"); | |
Iterator<String> iteratorC = tag.iterator(); | |
while (iteratorC.hasNext()) { | |
tags.add(iteratorC.next()); | |
} | |
List<Comment> comments = new ArrayList<>(); | |
JSONArray comment = (JSONArray) obj.get("comment"); | |
Iterator<JSONObject> iter = comment.iterator(); | |
while (iter.hasNext()) { | |
JSONObject com = iter.next(); | |
comments.add(Comment.toComment(com)); | |
} | |
return new YouTubeObj(id, title, des, thumbnail, tags, comments, cateID); | |
} | |
public String getId() { | |
return id; | |
} | |
public List<String> getTags() { | |
return tags; | |
} | |
public String getTitle() { | |
return title; | |
} | |
public String getDescription() { | |
return description; | |
} | |
public String getThumbnails() { | |
return thumbnails; | |
} | |
public List<Comment> getComments() { | |
return comments; | |
} | |
public String getCateID() { | |
return cateID; | |
} | |
} |