【问题标题】:JSON file Creation and adding nodes to JSON Tree type structureJSON 文件创建并将节点添加到 JSON 树类型结构
【发布时间】:2011-02-24 15:08:05
【问题描述】:

我编写了一个应用程序,它按照以下结构创建一个 JSON 文件:

{
  identifier:"id",
label:"name",
items:[
  {
     id:"ROOT",
     name:"Parent1",
     type:"ROOT",
     children:[
        {
           _reference:"CHILD1"
        }
     ]
  },
  {
     id:"CHILD1",
     name:"Parent1-Child1",
     type:"Child",
     children:[
        {
           _reference:"CHILD2"
        }
     ]
  },
  {
     id:"CHILD2",
     name:"Child1-Child2",
     type:"Child",
     children:[
        {
           _reference:"Child3"
        },
        {
           _reference:"Child4"
        }
     ]
  },
  {
     id:"Child3",
     name:"Child2-Child3",
     type:"GrandChild"
  },
  {
     id:"Child4",
     name:"Child2-Child4",
     type:"GrandChild"
  },

]
}

所以我的应用程序实际上获取了此 JSON 树的每个节点的以下数据

PID-- 我必须将子节点附加到它 CID--子节点的ID CNAME——子节点名称 CTYPE--子类型

所以目前我正在做的是通过迭代(在文件系统上)搜索 json 文件中的 PID,一旦我找到具有 id=PID 的字符串,我将一个子引用附加到该节点

    {
       _reference:"CHILD-NEXT"
    }

然后在这一行之后添加全新的子节点

{
 id:"CHILD-NEXT",
 name:"Parent1-ChildNext",
 type:"Child",
},

但这是非常低效的解决方案,因为每次都要迭代文件以搜索父节点并附加/插入子节点。

所以我正在寻找一种解决方案,我只需在内存中创建这个 JSON 结构,当我添加完所有节点后,我就可以将它写入文件。我在 JAVA 中执行此操作,因此我正在寻找一些现有的 JSON 库,可用于满足此要求。

如果您需要上述解释的任何其他信息不清楚,请发表评论。

【问题讨论】:

    标签: java json


    【解决方案1】:

    您可以使用org.json 库来执行此操作。

    您首先需要将您的 json 文件读入 JSONObject,它基本上由键/值对和数组组成。然后,您可以遍历对象中的项目,查找 pid 并添加新子项。

    这里有一些示例代码可以帮助您入门:

    //read file        
    BufferedReader in = new BufferedReader(new FileReader("path/json.txt"));
    String line;
    StringBuilder sb = new StringBuilder();
    while((line =in.readLine()) != null){
        sb.append(line);
    }
    in.close();
    
    
    //create a json object
    JSONObject json = new JSONObject(sb.toString());
    
    //pid to search for
    String pid = "XXX";
    
    //loop over the items array and look for pid
    JSONArray items = json.getJSONArray("items");
    for(int i = 0 ; i < items.length(); i++){
        JSONObject item = items.getJSONObject(i);
        String id = item.getString("id");
        if(pid.equals(id)){
    
            //get the children
            JSONArray children;
            if(item.has("children")){
                children = item.getJSONArray("children");    
            }
            else{
                children = new JSONArray();
                item.put("children", children);
            }
    
            //append a new child ref to the children
            JSONObject ref = new JSONObject();
            ref.put("_reference", "CHILD-NEXT");
            children.put(ref);
    
            //create a new child node
            JSONObject newItem = new JSONObject();
            newItem.put("id", "CHILD-NEXT");
            newItem.put("name", "Parent1-ChildNext");
            newItem.put("type", "Child");
            items.put(newItem);
        }
    }
    

    【讨论】:

    • 非常感谢,它确实帮助我获得了所需的解决方案。
    【解决方案2】:

    您可以从 JSON.org 获得 JSON in Java,它会为您提供内存中的 JSON 表示,您可以对其进行操作然后编写。

    This StackOverflow question 拥有完整的 Java JSON 库列表,包括 google-gsonJSONlibFlexJSONJacksonjson-simpleyet more on this blog review

    根据您是专注于全功能、轻量级还是快速,您可能会发现其中一个更适合您的需求。

    【讨论】:

      猜你喜欢
      • 2016-12-02
      • 2017-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-26
      相关资源
      最近更新 更多