2014-11-04 47 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

相关问题