java JSON 文件创建和添加节点到 JSON 树型结构
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5106450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSON file Creation and adding nodes to JSON Tree type structure
提问by Anil Vishnoi
I write an application which creates a JSON File following the below structure:
我编写了一个应用程序,它按照以下结构创建一个 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"
},
]
}
So my application actually gets following data for each node of this JSON Tree
所以我的应用程序实际上为这个 JSON 树的每个节点获取以下数据
PID-- It to which i have to attach the child node CID-- ID of Child Node CNAME-- Child Node Name CTYPE-- Child Type
PID--我必须附加到它的子节点 CID-- 子节点的 ID CNAME-- 子节点名称 CTYPE-- 子类型
So currently what i am doing is searching the PID in the json file through iterating ( on file system) and once i find the String which has id=PID,i append a Child Ref to that Node
所以目前我正在做的是通过迭代(在文件系统上)搜索 json 文件中的 PID,一旦我找到具有 id=PID 的字符串,我将一个子引用附加到该节点
{
_reference:"CHILD-NEXT"
}
and then after this line adding the whole new child node
然后在这一行之后添加整个新的子节点
{
id:"CHILD-NEXT",
name:"Parent1-ChildNext",
type:"Child",
},
But this is very inefficient solution because of iterating the file every time to search the parent node and appending/inserting the child.
但这是非常低效的解决方案,因为每次都迭代文件以搜索父节点并附加/插入子节点。
So i am looking for a kind of solution in which i just create this JSON structure in memory and when i am done with adding all the nodes then i can write it to the file. I am doing this in JAVA,so i am looking for some existing JSON Libraries which i can use to fulfil this requirement.
所以我正在寻找一种解决方案,我只是在内存中创建这个 JSON 结构,当我完成添加所有节点后,我可以将它写入文件。我正在 JAVA 中执行此操作,因此我正在寻找一些现有的 JSON 库,我可以使用它们来满足此要求。
Please comment if you need any other information of the above explanation is not clear.
如果您需要上述解释的任何其他信息,请发表评论。
回答by dogbane
You can use the org.jsonlibrary to do this.
您可以使用org.json库来执行此操作。
You first need to read your json file into a JSONObject
which is basically made up of key/value pairs and arrays. You can then loop over the items in the object, look for a pid and add a new child.
您首先需要将 json 文件读入一个JSONObject
基本上由键/值对和数组组成的文件。然后,您可以遍历对象中的项目,查找 pid 并添加一个新子项。
Here is some sample code to get you started:
以下是一些示例代码,可帮助您入门:
//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);
}
}
回答by justkt
From JSON.org you have JSON in Javathat will provide you an in-memory JSON representation that you can manipulate and then write.
在 JSON.org 上,你有Java中的JSON,它会为你提供一个内存中的 JSON 表示,你可以操作然后编写它。
This StackOverflow questionhas a whole list of JSON libraries for Java, including google-gson, JSONlib, FlexJSON, Hymanson, json-simple, and yet more on this blog review.
这个 StackOverflow 问题包含 Java 的 JSON 库的完整列表,包括google-gson、JSONlib、FlexJSON、Hymanson、json-simple,以及此博客评论中的更多内容。
Depending on whether you are focused on full-featured, lightweight, or fast, you may find one or the other more suited to your needs.
根据您是专注于全功能、轻量级还是快速,您可能会发现其中一种更适合您的需求。