Javascript 如何将一段文本转换为父子JSON文件?
我有一个由章节和子句组成的文本文件。是的 我想把它转换成类似下面的东西Javascript 如何将一段文本转换为父子JSON文件?,javascript,python,json,d3.js,Javascript,Python,Json,D3.js,我有一个由章节和子句组成的文本文件。是的 我想把它转换成类似下面的东西 {"name": "ROOT", "children": [ {"name": "Hemiptera", "children": [ {"name": "Miridae", "children": [ {"name": "Kanakamiris", "children":[]},
{"name": "ROOT",
"children": [
{"name": "Hemiptera",
"children": [
{"name": "Miridae",
"children": [
{"name": "Kanakamiris", "children":[]},
{"name": "Neophloeobia",
"children": [
{"name": "incisa", "children":[] }
]}
]}
]},
{"name": "Lepidoptera",
"children": [
{"name": "Nymphalidae",
"children": [
{"name": "Ephinephile",
"children": [
{"name": "rawnsleyi", "children":[] }
]}
]}
]}
]}
}
有没有一种方法可以用Javascript、Python或R编程实现这一点?首先,让我为您提出一种输入格式。这可能是:
var kenyaConstitutionArray = ["1#SOVEREIGNTY OF CONSTITUTION", "1:1#All sovereign...", "1:2#...",....,"100#....","100:1#..."]
其中只有1:1
表示第章,1:1
表示第1章的第一个子条款,1:1:1
表示第1章的第一个子条款。我使用了#
,因为我假设它不会出现在文本中
要获取章节和子句,您需要执行以下操作:
var path = text.substr(0, text.indexOf('#'));//it will give path or levels
这里,文本是数组的元素。例如,text=KenyaConstructionArray[1]
现在,您必须了解以下章节:
var chapter = path.substr(0, path.indexOf(':'));
以同样的方式获取子条款,只需稍加修改
并且,在循环中或递归地构建json
另一种方法是:
对于输入,也可以使用嵌套数组。比如:
var kenyaConstitution = [["chapter1",[["clause1", [["subclause1"], ["subclause2"]]],["clause2",[["subclause2-1"], ["subclause2-2"]]]]]];
将上述嵌套数组转换为json对您来说非常容易。在这种情况下,最好使用递归
编辑:
{
"name":"Constitution of Kenya",
"children":[
{
"name":"chapter1",
"children":[
{
"name":"clause1",
"children":[
{
"name":"subclause1",
"children":[
]
},
{
"name":"subclause2",
"children":[
]
}
]
},
{
"name":"clause2",
"children":[
{
"name":"subclause2-1",
"children":[
]
},
{
"name":"subclause2-2",
"children":[
]
}
]
}
]
}
]
}
完整代码:
{
"name":"Constitution of Kenya",
"children":[
{
"name":"chapter1",
"children":[
{
"name":"clause1",
"children":[
{
"name":"subclause1",
"children":[
]
},
{
"name":"subclause2",
"children":[
]
}
]
},
{
"name":"clause2",
"children":[
{
"name":"subclause2-1",
"children":[
]
},
{
"name":"subclause2-2",
"children":[
]
}
]
}
]
}
]
}
[忽略代码中的注释。]
<!DOCTYPE html>
<head>
<title>JSON</title>
<script>
function kenyaConstitutionToJSON() {
var kenyaConstitution = [["chapter1",[["clause1", [["subclause1"], ["subclause2"]]],["clause2",[["subclause2-1"], ["subclause2-2"]]]]]];
var kenyaChapterJSON;
var kenJSON = {};
kenJSON["name"] = "Constitution of Kenya";
kenJSON["children"] = [];
if(kenyaConstitution.length === 0) {
console.log("Constitution is empty! Please pass constitution through Parliament...");
return;
} else {
for (var chapter in kenyaConstitution) { //for each chapter return json
kenyaChapterJSON = convertToJSON(kenyaConstitution[chapter]) || {};
kenJSON["children"].push(kenyaChapterJSON);
}
}
return kenJSON;
}
function convertToJSON(constitutionArray) {
var obj = {};
//constitutionArray[item] = ["chapter1",[["clause1", [["subclause1"], ["subclause2"]]],["clause2",[["subclause2-1"], ["subclause2-2"]]]]]
obj["name"] = constitutionArray[0]; // {name: "children1", children=[ ]}
obj["children"] = [];
//if(constitutionArray.length > 0) {
for (var cl in constitutionArray[1]) {
var kenJSON1 = convertToJSON(constitutionArray[1][cl]);
obj["children"].push(kenJSON1);
}
//} else {
//obj["children"].push(constitutionArray[0]);
//}
return obj;
}
kenyaConstitutionToJSON();
</script>
</head>
<body>
</body>
希望这会有所帮助。kenya给出一个输入示例?您感兴趣的是JSON的格式,还是如何将源文档转换为嵌套容器?@jasonsharpe我感兴趣的是如何将源文档转换为嵌套容器containers@squint你可以用你想要的任何格式输入,简单就是prefferable@OdangaMadung我对一件事很好奇(如果它是机密的,不需要回答):肯尼亚JSON的宪法将如何帮助您的程序?感谢您对Imran的了解,您能编辑您的答案以包括将数组转换为JSON数据的递归方法吗?再次感谢