如何将一段文本转换为父子JSON文件?

2024-04-26 03:12:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个由章节和子句组成的文本文件。是constitution of Kenya。你知道吗

我想把它转换成类似于Flare.json的东西,如下所示。你知道吗

{"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编程实现?你知道吗


Tags: ofnamejsonroot文本文件children章节子句
1条回答
网友
1楼 · 发布于 2024-04-26 03:12:17

首先,让我为您提出一种输入格式。可能是这样的:

var kenyaConstitutionArray = ["1#SOVEREIGNTY OF CONSTITUTION", "1:1#All sovereign...", "1:2#...",....,"100#....","100:1#..."]

其中只有1#代表章节,1:1#代表第一章的第一个子条款,1:1:1#代表第一章的第一个子条款。我使用了#,因为我假设它不会出现在文本中。你知道吗

要获取章节和子句,需要执行以下操作:

var path = text.substr(0, text.indexOf('#'));//it will give path or levels

这里,文本是数组的元素。E、 例如,text = kenyaConstitutionArray[1]

现在,你必须得到一章:

var chapter = path.substr(0, path.indexOf(':'));

以同样的方式得到子条款,稍加修改

并且,在循环中或递归地构建json。你知道吗

另一种方法是:

对于输入,也可以使用嵌套数组。比如:

var kenyaConstitution = [["chapter1",[["clause1", [["subclause1"], ["subclause2"]]],["clause2",[["subclause2-1"], ["subclause2-2"]]]]]];

将上述嵌套数组转换为json对您来说非常容易。在这种情况下,最好使用递归。你知道吗

编辑:

完整代码:

[忽略代码中的注释。]

    <!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>

将断点放在return kenJSON;行上并查看输出。就像:

输出:

{
    "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":[

                            ]
                        }
                    ]
                }
            ]
        }
    ]
}

希望对你有帮助。你知道吗

相关问题 更多 >