gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:13:01 25 4
gpt4 key购买 nike

我正在尝试使用ajax将信息推送并发布到Django。在我的模型中,我只使用三个字段:id、parent 和 text。

当我尝试将信息发布回 Django 时,出现错误,因为 JSON 文件具有额外的字段,其中包含我的模型中没有的信息。

我希望能够仅从我拥有的 JSON 数据中收集字段 id、parent 和文本,以便我可以将其成功发布回 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();
};

这是我从变量“mytext”获取的示例 JSON

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

它将返回 Json 数组对象。现在,您可以使用每个临时变量从数组中获取 id、parent 和文本。

现在 stringfy 这个新变量并传递数据。

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

关于javascript - 将 javascript 中的 JSON 字段提取到变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36215518/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com