Skip to content
Permalink
0d82ff1dc4
Switch branches/tags

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?
Go to file
 
 
Cannot retrieve contributors at this time
144 lines (125 sloc) 3.59 KB
package kb.howtokb.wkhobject;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class Category_Json {
private int id;
private String data;
private int parent;
private List<Integer> children;
private List<Integer> rootpath;
public Category_Json(int id, String data, int parent, ArrayList<Integer> children, ArrayList<Integer> root){
this.id = id;
this.data = data;
this.parent = parent;
this.children = children;
this.rootpath = root;
}
public int getID(){
return id;
}
public void setID(int id){
this.id = id;
}
public String getData(){
return data;
}
public void setData(String data){
this.data = data;
}
public int getParent(){
return parent;
}
public void setParent(int id){
parent = id;
}
public List<Integer> getChildren(){
return children;
}
public void setChildren(ArrayList<Integer> child){
children = child;
}
public List<Integer> getRootpath(){
return rootpath;
}
public void setRootpath(ArrayList<Integer> root){
rootpath = root;
}
// ======================================================================
/**
* Return Json array object that convert from list of children
* @return JSONArray
*/
@SuppressWarnings("unchecked")
public JSONArray childrentoJsonArray(){
JSONArray category = new JSONArray();
if (getChildren().size() > 0){
for (int i=0; i<getChildren().size(); i++){
category.add(getChildren().get(i));
}
}
return category;
}
// ======================================================================
/**
* Return Json array object that convert from rootpath
* @return JSONArray
*/
@SuppressWarnings("unchecked")
public JSONArray rootpathtoJsonArray(){
JSONArray category = new JSONArray();
if (getRootpath().size() > 0){
for (int i=0; i<getRootpath().size(); i++){
category.add(getRootpath().get(i));
}
}
return category;
}
// ======================================================================
/**
* Return Json object that convert from a category
* @return JSONArray
*/
@SuppressWarnings("unchecked")
public JSONObject categoryToJsonObject(){
JSONObject obj = new JSONObject();
obj.put("data", getData());
obj.put("id", getID());
obj.put("parent", getParent());
obj.put("children", childrentoJsonArray());
obj.put("rootpath", rootpathtoJsonArray());
return obj;
}
static final JSONParser parser = new JSONParser();
public static Category_Json fromJson(String json) throws ParseException{
Object obj = parser.parse(json);
JSONObject jsonObject = (JSONObject) obj;
return jsontoCategory(jsonObject);
}
private static Category_Json jsontoCategory(JSONObject obj){
String data = (String) obj.get("data");
int id = (int) (long) obj.get("id");
int parent = (int) (long) obj.get("parent");
//System.out.println(categoryid.get(parent));
ArrayList<Integer> children = new ArrayList<>();
JSONArray childrenJ = (JSONArray) obj.get("children");
Iterator<Long> iteratorC = childrenJ.iterator();
while (iteratorC.hasNext()) {
children.add((int) (long) iteratorC.next());
}
ArrayList<Integer> rootpath = new ArrayList<>();
JSONArray rootpathJ = (JSONArray) obj.get("rootpath");
Iterator<Long> iteratorR = rootpathJ.iterator();
while (iteratorR.hasNext()) {
rootpath.add((int) (long) iteratorR.next());
}
//System.out.println("test");
Category_Json newcate = new Category_Json(id,
data, parent , children, rootpath);
return newcate;
}
}