2014-11-04 68 views
-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]} 

回答

1

如果你想反序列化JSON字符串轉換成Java對象,簡單的代碼下面可以幫助您。

也從這個答案下面的評論,我明白你需要保持在父母的引用在兒童。

1)定義POJO:

public class Item { 
    private String name; 
    private String id; 
    private String photo; 
    private Item[] child; 
    private Item parent; 

    public void defineParent(Item parent) { 
     this.parent = parent; 
     if(this.child != null && this.child.length > 0) { 
      for (Item currentChild : this.child) { 
       currentChild.defineParent(this); 
      } 
     } 
    } 

    // TODO: getters/setters 
} 

2)反序列化JSON你把它:

String 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\"}]}]}"; 
    Gson gson = new Gson(); 
    Item item = gson.fromJson(json, Item.class); 
    item.defineParent(null); 

編輯:父母準備通過defineParent方法遞歸設置爲兒童。所以每個孩子都知道它的父母。

+0

感謝您的回覆,但我已經定義了一個POJO作爲您的,我的json文件來加載資產,問題是我有菜單,你必須從我的json文件自動創建,爲此我需要在父子之間創建引用,這樣當我點擊孩子時,通過輸入一個特殊的bean將會讓他的孩子(如果有)。對不起我的英語不好。 – 2014-11-04 21:14:39

+0

@尼克很難理解你需要什麼,但我想你想讓每個孩子都知道它的父母,這樣你就可以通過父母到孩子和孩子到父母。對不起,如果我誤解了你。 – Devrim 2014-11-04 21:38:38

+0

究竟@DevrimTuncer – 2014-11-04 21:54:57

相關問題