【问题标题】:GSON - Creating JSON tree structure from JavaGSON - 从 Java 创建 JSON 树结构
【发布时间】:2012-06-25 17:34:49
【问题描述】:

有几个问题,使用 GSON。我感觉 GSON 可能不是我正在寻找的库,它能够为我提供一个我以后可以使用的 JSON 对象。

我正在从数据库中读取数据以填充稍后将使用的 json 对象。 json 对象的输出应该类似于下面的 json,其中涉及到父母和孩子。它形成了一个基于小树的结构:

var json = { 
         id: "1", 
         name: "Joe Smith", 
         data: { 
         "email": "",
         "phone": "123-123-1233"},
         children: [{
                        id: "Tim Anderson",
                        name: "Tim Anderson",
                        data: { 
                             "email": "x@gmail.com",
                             "phone": "123-123-1233"
                        },
                        children: []
                    },{
                        id: "Christopher Johnson",
                        name: "Christopher Johnson",
                        data: {  
                             "email": "x@gmail.com",
                             "phone": "123-123-1233"
                        },
                        children: []
                    },{
                        id: "Kate Green",
                        name: "Kate Green",
                        data: {

                        },
                        children: [{
                            id: "Gary Jones",
                            name: "Gary Jones",
                            data: {},
                            children: []
                        }, {
                            id: "Melissa Brand",
                            name: "Melissa Brand",
                            data: {},
                            children: []
                        }]
                    }
                    ] 
    }

如何创建一个类似于上述结构的 GSON 对象,我可以将其序列化为具有这种层次结构的 JSON?我尝试过使用地图和其他集合 - 但我很难获得我想要的结果。因此,我为什么要问 GSON 是否是我真正想要的 JSON 序列化。

【问题讨论】:

    标签: java json gson


    【解决方案1】:

    你试过默认的javaJSONTokener和其他类似的解析器吗?

          BufferedReader reader = new BufferedReader(new FileReader("jsonfile.json"));
         StringBuilder builder=new StringBuilder();
         for(String line=null;(line = reader.readLine()) != null;){
         builder.append(line).append("\n");
         }
        JSONTokener jsonTokener=new JSONTokener(builder.toString());
        JSONObject finalJson=new JSONObject(jsonTokener);
    

    在你的情况下,如果你想在里面的层次结构中找到额外的数据,然后迭代 如下

     JSONArray children=finalJson.getJSONArray("children");
     for(int i = 0;i<children.length;i++){
     JSONObject childRecData = children.getJSONObject(i); //returns the ith JSONObject containing id, name, data, etc.
     int id = childRecData.getString();
     String name = childRecData.getString();
     JSONObject data = childRecData.getJSONObject(0);
     }
    

    注意对于较大的 JSON 文件(具有荒谬层次结构的文件),例如 HTTP 数据库请求,建议使用 Nishant 建议的 GSON 的 POJO(普通旧 Java 对象)模型

    这里是more information

    这是similar question

    【讨论】:

      【解决方案2】:

      让我给你一个提示

      像这样创建一个 Person 和一个 Data 类 (POJO)

      Person
        String id
        String name
        Data data
        List<Person> children
      
      Data
        String email
        String phone
      

      【讨论】:

      • 谢谢 - 我看到我可以使用 POJO 对象在树中创建节点的实例。我可以在 GSON 序列化中毫无问题地使用这些对象吗?
      • @K82_Med 是的。您可以使用它们进行序列化和反序列化。
      • 谢谢你——感谢你的回答,我知道我要去哪里了。感谢您的提示。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-01
      • 1970-01-01
      • 2019-08-20
      • 1970-01-01
      • 1970-01-01
      • 2017-07-19
      • 2012-12-14
      相关资源
      最近更新 更多