เป็น code ที่ผมเอาไว้อ่านข้อมูลจาก JSON tree ที่มีโครงสร้างข้อมูลเหมือนกัน ครับ
สังเกตว่าทุก node จะมีโครงสร้างข้อมูลเหมือนกัน คือมี name และ children เหมือนกัน
service.json
{
"children": [
{
"name": "การจัดบริการ",
"children": [
{ "name": "บริการโรคทั่วไป" }
]
},
{
"name": "คลินิกเฉพาะทาง/โรค",
"children": [
{
"name": "อายุรกรรม",
"children": [
{ "name": "คลินิกโรคหัวใจ" },
{ "name": "คลินิกโรคไต" },
{ "name": "คลินิกโรคระบบทางเดินอาหาร" },
{ "name": "คลินิกโรคระบบการหายใจ / คลินิกโรคทรวงอก" }
]
}
]
},
{
"name" : "บริการเฉพาะ",
"children" : [
{ "name" : "การบริการโรคเลือดออกง่ายฮีโมฟีเลีย" },
{ "name" : "การบริการบำบัดยาเสพติด" },
{ "name" : "การบริการรังสีรักษา" }
]
}
]
}
ใช้ code ที่ผมเขียนไว้ อ่านข้อมูลจาก file service.json ขึ้นมาString path = this.getClass().getResource("/lookup/service.json").getPath();
new JSONWalker(new File(path)) { //class นี้ผมเขียนขึ้นมา code อยู่ข้างล่าง
@Override
public void walking(JSONWalker.Data data) {
LOG.debug("id --> {}", data.getId());
LOG.debug("name --> {}", data.getName());
LOG.debug("parent --> {}", data.getParent());
LOG.debug("");
}
}.walk();
ผลลัพธ์ที่ได้ จะแสดงผลดังนี้- id--> 1 - name--> การจัดบริการ - parent--> null - id--> 1.1 - name--> บริการโรคทั่วไป - parent--> 1 - id--> 2 - name--> คลินิกเฉพาะทาง/โรค - parent--> null - id--> 2.1 - name--> อายุรกรรม - parent--> 2 - id--> 2.1.1 - name--> คลินิกโรคหัวใจ - parent--> 2.1 - id--> 2.1.2 - name--> คลินิกโรคไต - parent--> 2.1 - id--> 2.1.3 - name--> คลินิกโรคระบบทางเดินอาหาร - parent--> 2.1 - id--> 2.1.4 - name--> คลินิกโรคระบบการหายใจ / คลินิกโรคทรวงอก - parent--> 2.1 - id--> 3 - name--> บริการเฉพาะ - parent--> null - id--> 3.1 - name--> การบริการโรคเลือดออกง่ายฮีโมฟีเลีย - parent--> 3 - id--> 3.2 - name--> การบริการบำบัดยาเสพติด - parent--> 3 - id--> 3.3 - name--> การบริการรังสีรักษา - parent--> 3
โค๊ดเต็ม
JSONWalker.java
package com.blogspot.na5cecnt.util;
import java.io.File;
import java.util.Objects;
import org.apache.commons.io.FileUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
*
* @author redcrow
*/
public abstract class JSONWalker {
private final File jsonFile;
public JSONWalker(File jsonFile) {
this.jsonFile = jsonFile;
}
public abstract void walking(Data data);
public void walk() throws Exception {
String json = FileUtils.readFileToString(jsonFile, "utf-8");
walkingTree(new JSONObject(json), "1", null);
}
private void walkingTree(JSONObject object, String id, String parentId) throws Exception {
try {
String name = object.getString("name");
boolean hasChildren;
try {
object.getJSONArray("children");
hasChildren = true;
} catch (JSONException ex) {
hasChildren = false;
}
walking(new Data(id, name, parentId, object, hasChildren));
} catch (JSONException ex) {
id = null;
}
JSONArray jsonArray;
try {
jsonArray = object.getJSONArray("children");
} catch (JSONException ex) {
return;
}
int length = jsonArray.length();
for (int i = 1; i < length + 1; i++) {
JSONObject child = jsonArray.getJSONObject(i - 1);
walkingTree(child, (id == null) ? (i + "") : (id + "." + i), id);
}
}
public static class Data {
private final String id;
private final String name;
private final String parent;
private boolean hasChildren = false;
private final JSONObject object;
public Data(String id, String name, String parent, JSONObject object, boolean hasChildren) {
this.id = id;
this.name = name;
this.parent = parent;
this.object = object;
this.hasChildren = hasChildren;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getParent() {
return parent;
}
public JSONObject getObject() {
return object;
}
public boolean hasChildren() {
return hasChildren;
}
@Override
public int hashCode() {
int hash = 7;
hash = 89 * hash + Objects.hashCode(this.id);
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Data other = (Data) obj;
if (!Objects.equals(this.id, other.id)) {
return false;
}
return true;
}
}
}
dependencies ที่จำเป็นต้องใช้ เมื่อคุณใช้ maven
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
</dependency>

ไม่มีความคิดเห็น:
แสดงความคิดเห็น