将 javascript 对象或数组转换为 json 以获取 ajax 数据 [英] Convert javascript object or array to json for ajax data
问题描述
所以我正在创建一个包含元素信息的数组.我遍历所有元素并保存索引.由于某种原因,我无法将此数组转换为 json 对象!
So I'm creating an array with element information. I loop through all elements and save the index. For some reason I cannot convert this array to a json object!
这是我的数组循环:
var display = Array();
$('.thread_child').each(function(index, value){
display[index]="none";
if($(this).is(":visible")){
display[index]="block";
}
});
我尝试通过以下方式将其转换为 JSON 对象:
I try to turn it into a JSON object by:
data = JSON.stringify(display);
它似乎没有发送正确的 JSON 格式!
It doesn't seem to send the proper JSON format!
如果我像这样手动编写代码,它就可以工作并发送信息:
If I hand code it like this, it works and sends information:
data = {"0":"none","1":"block","2":"none","3":"block","4":"block","5":"block","6":"block","7":"block","8":"block","9":"block","10":"block","11":"block","12":"block","13":"block","14":"block","15":"block","16":"block","17":"block","18":"block","19":"block"};
当我对 JSON.stringify 对象发出警报时,它看起来与手动编码的对象相同.但它不起作用.
When I do an alert on the JSON.stringify object it looks the same as the hand coded one. But it doesn't work.
试图解决这个问题我快疯了!我在这里错过了什么?发送此信息以获取手动编码格式的最佳方式是什么?
I'm going crazy trying to solve this! What am I missing here? What's the best way to send this information to get the hand coded format?
我使用这个ajax方法发送数据:
I am using this ajax method to send data:
$.ajax({
dataType: "json",
data:data,
url: "myfile.php",
cache: false,
method: 'GET',
success: function(rsp) {
alert(JSON.stringify(rsp));
var Content = rsp;
var Template = render('tsk_lst');
var HTML = Template({ Content : Content });
$( "#task_lists" ).html( HTML );
}
});
使用 GET 方法是因为我正在显示信息(不更新或插入).只向我的 php 文件发送显示信息.
Using GET method because I'm displaying information (not updating or inserting). Only sending display info to my php file.
最终解决方案
var display = {};
$('.thread_child').each(function(index, value){
display[index]="none";
if($(this).is(":visible")){
display[index]="block";
}
});
$.ajax({
dataType: "json",
data: display,
url: "myfile.php",
cache: false,
method: 'GET',
success: function(rsp) {
alert(JSON.stringify(rsp));
var Content = rsp;
var Template = render('tsk_lst');
var HTML = Template({ Content : Content });
$( "#task_lists" ).html( HTML );
}
});
推荐答案
我不完全确定,但我认为您可能对数组在 JSON 中的序列化方式感到惊讶.让我们隔离问题.考虑以下代码:
I'm not entirely sure but I think you are probably surprised at how arrays are serialized in JSON. Let's isolate the problem. Consider following code:
var display = Array();
display[0] = "none";
display[1] = "block";
display[2] = "none";
console.log( JSON.stringify(display) );
这将打印:
["none","block","none"]
这就是 JSON 实际序列化数组的方式.但是,您想看到的是:
This is how JSON actually serializes array. However what you want to see is something like:
{"0":"none","1":"block","2":"none"}
要获得这种格式,您需要序列化对象,而不是数组.所以让我们像这样重写上面的代码:
To get this format you want to serialize object, not array. So let's rewrite above code like this:
var display2 = {};
display2["0"] = "none";
display2["1"] = "block";
display2["2"] = "none";
console.log( JSON.stringify(display2) );
这将以您想要的格式打印.
This will print in the format you want.
你可以在这里玩这个:http://jsbin.com/oDuhINAG/1/编辑?js,控制台
这篇关于将 javascript 对象或数组转换为 json 以获取 ajax 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!