从表单

时间:2018-05-26 14:31:43

标签: javascript jquery arrays json

此刻我正在尝试创建一个像这样的json。

[
{"name": "set registry key right",
"win_acl": {
  "path": "HKCU:\\Bovine\\Key",
  "user": "BUILTIN\\Users",
  "rights": "EnumerateSubKeys",
  "type": "allow",
  "state": "present",
  "inherit": "ContainerInherit, ObjectInherit",
  "propagation": "None"
}
},
{
"name": "Remove FullControl AccessRule for IIS_IUSRS",
"win_acl": {
  "path": "C:\\inetpub\\wwwroot\\MySite",
  "user": "IIS_IUSRS",
  "rights": "FullControl",
  "type": "allow",
  "state": "absent",
  "inherit": "ContainerInherit, ObjectInherit",
  "propagation": "None"
}
}
]

我想通过javascript动态创建它。

这就是我现在所拥有的:

    function GenerateYaml(btn) {
    $('#generatedYamlTextField').removeAttr("hidden");

    var id = btn.replace("generateBtn", "");
    var moduleName = $("#formpanel" + id).attr("data-title-caption");

    //Looping trough panels
    $("#formpanel" + id).each(function () {

        var json = "[\n{\n\"name\":\"" + "module beschrijving" + "\",\n";
        json += "\"" + moduleName + "\": {\n";
        //Looping through labels in the panel to create the object
        $('label').each(function (index, value) {
                            var is_last_item = (index == ($('label').length - 1));

            if (!is_last_item) {
                json += "\"" + value.innerText + "\":"+"\"textboxvalue\",\n";
            } else {

                json += "\"" + value.innerText + "\":"+"\"textboxvalue\"\n";

            }

        });

        json += "}\n},]\n";
        $("#yamltextfield").append(json);

    });   
}

这是我在textarea上面的代码中获得的:

[
{
"name":"module beschrijving",
"win_acl_inheritance_module": {
"path":"textboxvalue",
"reorganize":"textboxvalue",
"state":"textboxvalue"
}
},]

我的问题是我有多个面板,我想将它们添加到同一个数组中,这样我就可以像首先显示的json一样。

我希望你们能帮助我。提前谢谢。

问候, Mouaad

1 个答案:

答案 0 :(得分:0)

不要手动形成json字符串。只需撰写你的对象,把它们放在一个数组中,比如说self.eventStore.requestAccess(to: EKEntityType.reminder) { (granted, error) in } 你可以得到json字符串:

arr