将 javascript 中的 JSON 字段提取到变量

Extract JSON fields in javascript to a variable

我正在尝试使用 ajax 向 Django 推送和 post 信息。在我的模型中,我只使用了三个字段:id、parent 和 text。

当我尝试 post 将信息返回给 Django 时,我遇到了一个错误,因为 JSON 文件有额外的字段,其中包含我的模型中没有的信息。

我希望能够仅从我拥有的 JSON 数据中收集字段 id、parent 和文本,以便我可以 post 将其成功返回到 Django。

这是我当前的代码:

    $('#jstree_demo').jstree({
            "core" : {
                "animation" : 0,
                "check_callback" : true,
                "themes" : { "stripes" : true },
                'data' : {
                  "url" : "/snippets/",
                  "success": function(data){
                     series = data;
            },
                  "dataType" : "json" // needed only if you do not supply JSON headers
                }
            },

            "plugins" : [ "contextmenu", "dnd", "search", "state", "types", "wholerow" ]
        });
});


function get_tree(){

    var v = $('#jstree_demo').jstree(true).get_json('#', {flat:true})
    var mytext = JSON.stringify(v);

    $.ajax({
      url:/snippets/,
      type:"POST",
      data:mytext,
      contentType:"application/json; charset=utf-8",
      dataType:"json",

    })

}


function demo_create() {
    var ref = $('#jstree_demo').jstree(true),
        sel = ref.get_selected();
    if(!sel.length) { return false; }
    sel = sel[0];
    sel = ref.create_node(sel, {"type":"default"});
    if(sel) {
        ref.edit(sel);
    }

    get_tree();
};
function demo_rename() {
    var ref = $('#jstree_demo').jstree(true),
        sel = ref.get_selected();
    if(!sel.length) { return false; }
    sel = sel[0];
    ref.edit(sel);

    get_tree();
};
function demo_delete() {
    var ref = $('#jstree_demo').jstree(true),
        sel = ref.get_selected();
    if(!sel.length) { return false; }
    ref.delete_node(sel);

    get_tree();
};

这是示例 JSON 我从变量 "mytext"

[{"id":"node_one","text":"say its so","icon":true,"li_attr":{"id":"node_one"},"a_attr":{"href":"#","id":"node_one_anchor"},"state":{"loaded":true,"opened":true,"selected":false,"disabled":false},"data":{},"parent":"#","type":"default"},{"id":"some","text":"Ivan is sleepy","icon":true,"li_attr":{"id":"some"},"a_attr":{"href":"#","id":"some_anchor"},"state":{"loaded":true,"opened":false,"selected":false,"disabled":false},"data":{},"parent":"node_one","type":"default"},{"id":"node_1","text":"Node","icon":true,"li_attr":{"id":"node_1"},"a_attr":{"href":"#","id":"node_1_anchor"},"state":{"loaded":true,"opened":false,"selected":true,"disabled":false},"data":{},"parent":"#","type":"default"},{"id":"j1_10","text":"New node","icon":true,"li_attr":{"id":"j1_10"},"a_attr":{"href":"#"},"state":{"loaded":true,"opened":false,"selected":false,"disabled":false},"data":{},"parent":"node_1","type":"default"},{"id":"node1","text":"test worked","icon":true,"li_attr":{"id":"node1"},"a_attr":{"href":"#","id":"node1_anchor"},"state":{"loaded":true,"opened":false,"selected":false,"disabled":false},"data":{},"parent":"#","type":"default"},{"id":"j1_1","text":"New node","icon":true,"li_attr":{"id":"j1_1"},"a_attr":{"href":"#"},"state":{"loaded":true,"opened":false,"selected":false,"disabled":false},"data":{},"parent":"node1","type":"default"},{"id":"node2","text":"test worked 2","icon":true,"li_attr":{"id":"node2"},"a_attr":{"href":"#","id":"node2_anchor"},"state":{"loaded":true,"opened":false,"selected":false,"disabled":false},"data":{},"parent":"#","type":"default"},{"id":"j1_9","text":"New node","icon":true,"li_attr":{"id":"j1_9"},"a_attr":{"href":"#"},"state":{"loaded":true,"opened":false,"selected":false,"disabled":false},"data":{},"parent":"node2","type":"default"},{"id":"node3","text":"test worked yay","icon":true,"li_attr":{"id":"node3"},"a_attr":{"href":"#","id":"node3_anchor"},"state":{"loaded":true,"opened":false,"selected":false,"disabled":false},"data":{},"parent":"#","type":"default"},{"id":"j1_2","text":"New node","icon":true,"li_attr":{"id":"j1_2"},"a_attr":{"href":"#"},"state":{"loaded":true,"opened":false,"selected":false,"disabled":false},"data":{},"parent":"node3","type":"default"},{"id":"j1_7","text":"New node","icon":true,"li_attr":{"id":"j1_7"},"a_attr":{"href":"#"},"state":{"loaded":true,"opened":false,"selected":false,"disabled":false},"data":{},"parent":"j1_2","type":"default"},{"id":"j1_8","text":"New node","icon":true,"li_attr":{"id":"j1_8"},"a_attr":{"href":"#"},"state":{"loaded":true,"opened":false,"selected":false,"disabled":false},"data":{},"parent":"j1_2","type":"default"},{"id":"j1_3","text":"New node","icon":true,"li_attr":{"id":"j1_3"},"a_attr":{"href":"#"},"state":{"loaded":true,"opened":true,"selected":false,"disabled":false},"data":{},"parent":"node3","type":"default"},{"id":"j1_4","text":"New node","icon":true,"li_attr":{"id":"j1_4"},"a_attr":{"href":"#"},"state":{"loaded":true,"opened":false,"selected":false,"disabled":false},"data":{},"parent":"j1_3","type":"default"},{"id":"j1_6","text":"New node","icon":true,"li_attr":{"id":"j1_6"},"a_attr":{"href":"#"},"state":{"loaded":true,"opened":false,"selected":false,"disabled":false},"data":{},"parent":"j1_4","type":"default"},{"id":"j1_5","text":"New node","icon":true,"li_attr":{"id":"j1_5"},"a_attr":{"href":"#"},"state":{"loaded":true,"opened":false,"selected":false,"disabled":false},"data":{},"parent":"j1_3","type":"default"}]

我正在尝试与 jsTree 协调来完成这项工作。非常感谢任何帮助。

使用 var mytext = JSON.parse(v); 而不是 stringify。

它将return Json数组对象。现在您可以将数组中的 id、parent 和文本分别使用到一个临时变量中。

现在将这个新变量字符串化并传递数据。

希望这能帮助您解决问题。