GSON-从Java创建JSON树结构

GSON-从Java创建JSON树结构,java,json,gson,Java,Json,Gson,用GSON问几个问题。我有一种感觉,GSON可能不是我想要的库,它可以给我一个JSON对象,我以后可以使用 我从数据库中读取数据以填充稍后将使用的json对象。json对象的输出应该类似于下面的json,它涉及父对象和子对象。它形成一个基于树的小结构: var json = { id: "1", name: "Joe Smith", data: { "email": "", "phone": "12

用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序列化

让一个人和一个数据类(POJO)像这样

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

Data
  String email
  String phone
人
字符串id
字符串名
数据数据
列出孩子
资料
字符串电子邮件
串电话

您是否尝试过默认的java
JSONTokener
和其他类似的解析器

      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);
 }
JSONArray children=finalJson.getJSONArray(“children”);

for(int i=0;iThanks-我知道我可以使用POJO对象在树中创建节点实例。我可以在GSON序列化中使用这些对象而不会出现问题吗?@K82\u Med是的。你可以使用它们来序列化和反序列化。谢谢你-多亏了你的回答,我知道我要去哪里了。谢谢你的提示。