Created
May 21, 2016 18:04
-
-
Save jeggy/64678f115a731ce7d0e730173a61fd07 to your computer and use it in GitHub Desktop.
Todo tree Api json reading
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
import com.google.gson.JsonArray; | |
import com.google.gson.JsonObject; | |
import com.google.gson.JsonParser; | |
import java.util.ArrayList; | |
/** | |
* Created by Jógvan 21/05-2016 19:28. | |
* | |
* Using this library: http://www.java2s.com/Code/JarDownload/json-simple/json-simple-1.1.1.jar.zip | |
*/ | |
public class json { | |
static String jsondata = "{\n" + | |
" \"todos\": [\n" + | |
" {\n" + | |
" \"_id\": \"572c5f17e2cce1e442ff1c9c\",\n" + | |
" \"title\": \"Root!\",\n" + | |
" \"owner\": \"5729fec3b2102fdd1dd53a32\",\n" + | |
" \"__v\": 2,\n" + | |
" \"child\": [\n" + | |
" {\n" + | |
" \"_id\": \"572c5f930b63dee544e1eabf\",\n" + | |
" \"title\": \"Parent2!\",\n" + | |
" \"owner\": \"5729fec3b2102fdd1dd53a32\",\n" + | |
" \"__v\": 2,\n" + | |
" \"child\": [\n" + | |
" {\n" + | |
" \"_id\": \"572c5fad5993c09c451c31a2\",\n" + | |
" \"title\": \"Child4!\",\n" + | |
" \"owner\": \"5729fec3b2102fdd1dd53a32\",\n" + | |
" \"__v\": 1,\n" + | |
" \"child\": [\n" + | |
" {\n" + | |
" \"_id\": \"572c5fbb841f2a684698de1e\",\n" + | |
" \"title\": \"Super Child!\",\n" + | |
" \"owner\": \"5729fec3b2102fdd1dd53a32\",\n" + | |
" \"__v\": 0,\n" + | |
" \"child\": [],\n" + | |
" \"parent\": \"572c5fad5993c09c451c31a2\",\n" + | |
" \"root\": \"572c5f17e2cce1e442ff1c9c\",\n" + | |
" \"archived\": false,\n" + | |
" \"date\": \"2016-05-06T09:11:23.704Z\"\n" + | |
" }\n" + | |
" ],\n" + | |
" \"parent\": \"572c5f930b63dee544e1eabf\",\n" + | |
" \"root\": \"572c5f17e2cce1e442ff1c9c\",\n" + | |
" \"archived\": false,\n" + | |
" \"date\": \"2016-05-06T09:11:09.090Z\"\n" + | |
" }\n" + | |
" ],\n" + | |
" \"parent\": \"572c5f17e2cce1e442ff1c9c\",\n" + | |
" \"root\": \"572c5f17e2cce1e442ff1c9c\",\n" + | |
" \"archived\": false,\n" + | |
" \"date\": \"2016-05-06T09:10:43.367Z\"\n" + | |
" }\n" + | |
" ],\n" + | |
" \"parent\": null,\n" + | |
" \"root\": null,\n" + | |
" \"archived\": false,\n" + | |
" \"date\": \"2016-05-06T09:08:39.732Z\"\n" + | |
" },\n" + | |
" {\n" + | |
" \"_id\": \"572c760f880b964e7ac06085\",\n" + | |
" \"title\": \"Root2!\",\n" + | |
" \"owner\": \"5729fec3b2102fdd1dd53a32\",\n" + | |
" \"__v\": 1,\n" + | |
" \"child\": [],\n" + | |
" \"parent\": null,\n" + | |
" \"root\": null,\n" + | |
" \"archived\": false,\n" + | |
" \"date\": \"2016-05-06T10:46:39.187Z\"\n" + | |
" }\n" + | |
" ]\n" + | |
"}"; | |
public static void main(String[] args) { | |
new json(); | |
} | |
public json(){ | |
JsonObject o = new JsonParser().parse(jsondata).getAsJsonObject(); | |
JsonArray roots = o.getAsJsonArray("todos"); | |
ArrayList<Todo> todoRoots = new ArrayList<>(); | |
for (int i = 0; i < roots.size(); i++) { | |
todoRoots.add(build(roots.get(i).getAsJsonObject(), null)); | |
} | |
for (Todo todo : todoRoots) { | |
printTodos(todo, 0); | |
} | |
} | |
// Print all | |
public void printTodos(Todo todo, int spacing){ | |
for (int i = 0; i < spacing; i++) { | |
System.out.print("-"); | |
} | |
System.out.println(todo.title); | |
for (Todo child : todo.children) { | |
printTodos(child, spacing+1); | |
} | |
} | |
// Load all todos | |
public Todo build(JsonObject currentNode, Todo parent){ | |
String title = currentNode.get("title").getAsString(); | |
boolean achieved = currentNode.get("archived").getAsBoolean(); | |
Todo todo = new Todo(title, achieved, null, null); | |
if(parent != null){ | |
todo.parent = parent; | |
if(parent.root != null){ | |
todo.root = parent.root; | |
}else { | |
todo.root = parent; | |
} | |
} | |
JsonArray children = currentNode.getAsJsonArray("child"); | |
for (int i = 0; i < children.size(); i++) { | |
todo.children.add(build(children.get(i).getAsJsonObject(), todo)); | |
} | |
return todo; | |
} | |
// Todo class | |
class Todo{ | |
String title; | |
boolean achieved; | |
Todo root; | |
Todo parent; | |
ArrayList<Todo> children = new ArrayList<>(); | |
public Todo(String title, boolean achieved, Todo root, Todo parent){ | |
this.title = title; | |
this.achieved = achieved; | |
this.root = root; | |
this.parent = parent; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment