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/wkhobject/Category_Json.java
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
144 lines (125 sloc)
3.59 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.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; | |
} | |
} |