-3
這是我創建的方法,一切都很好,但我無法將他的父親與HashMap hm中的孩子關聯起來。我希望有人幫助我。謝謝。我如何將他們的孩子與他的標題聯繫起來? Gson Java
public void findChild(JsonArray jarray) {
ArrayList<String> listChild = new ArrayList<String>();
HashMap<String, List<String>> hm = new HashMap<String, List<String>>();
String str = null;
JsonElement element = null;
JsonElement e;
for (int j = 0; j < jarray.size(); j++) {
e = jarray.get(j).getAsJsonObject().get("name");
JsonObject jsonObj = jarray.get(j).getAsJsonObject();
JsonArray jsonArray = jsonObj.getAsJsonArray("child");
element = jsonObj.get("name");
if (jsonArray.size() > 0) {
str = e.getAsString();
findAllChild(jsonArray);
}
listChild.add(element.getAsString());
}
hm.put(str, listChild);
System.err.println("Hash: " + hm);
}
這是我的JSON文件:
"name": "one",
"id":"YO",
"child": [
{
"name": "one",
"child": [
{
"id": "0001",
"name": "oneone",
"photo": "primo.jpg",
"child": [
{
"id": "1",
"name": "oneoneone",
"child": [
{
"id": "1",
"name": "oneoneoneone",
"child": [
{
"id": "1",
"name": "oneoneoneoneone"
},
{
"id": "2",
"name": "oneoneoneonetwo"
},
{
"id": "3",
"name": "oneoneoneonethree"
}
]
},
{
"id": "2",
"name": "oneoneonetwo"
},
{
"id": "3",
"name": "oneoneonethree"
}
]
},
{
"id": "2",
"name": "oneonetwo"
}
]
},
{
"id": "0002",
"name": "onetwo",
"photo": "secondo.jpg"
},
{
"id": "onethree",
"name": "terzo",
"photo": "terzo.jpg"
}
]
},
{
"name": "two",
"child": [
{
"id": "0004",
"name": "twoone",
"photo": "one.jpg"
},
{
"id": "0005",
"name": "twotwo",
"photo": "two.jpg",
"child": [
{
"id": "1",
"name": "twotwoone",
"child": [
{
"id": "1",
"name": "twotwooneone"
},
{
"id": "2",
"name": "twotwoonetwo"
},
{
"id": "3",
"name": "twotwoonethree"
}
]
},
{
"id": "2",
"name": "twotwotwo"
}
]
},
{
"id": "0006",
"name": "twothree",
"photo": "three.jpg"
}
]
}
]
}
這是我的迴應:
Hash: {null=[oneoneoneoneone, oneoneoneonetwo, oneoneoneonethree]}
Hash: {oneoneoneone=[oneoneoneone, oneoneonetwo, oneoneonethree]}
Hash: {oneoneone=[oneoneone, oneonetwo]}
Hash: {oneone=[oneone, onetwo, terzo]}
Hash: {null=[twotwooneone, twotwoonetwo, twotwoonethree]}
Hash: {twotwoone=[twotwoone, twotwotwo]}
Hash: {twotwo=[twoone, twotwo, twothree]}
Hash: {two=[one, two]}
感謝您的回覆,但我已經定義了一個POJO作爲您的,我的json文件來加載資產,問題是我有菜單,你必須從我的json文件自動創建,爲此我需要在父子之間創建引用,這樣當我點擊孩子時,通過輸入一個特殊的bean將會讓他的孩子(如果有)。對不起我的英語不好。 – 2014-11-04 21:14:39
@尼克很難理解你需要什麼,但我想你想讓每個孩子都知道它的父母,這樣你就可以通過父母到孩子和孩子到父母。對不起,如果我誤解了你。 – Devrim 2014-11-04 21:38:38
究竟@DevrimTuncer – 2014-11-04 21:54:57