转换的JavaScript对象或数组JSON阿贾克斯数据 [英] Convert javascript object or array to json for ajax data
问题描述
所以我创建与要素信息的数组。我经历了所有的元素循环,并保存索引。出于某种原因,我这个数组不能转换为一个JSON对象!
这是我的数组循环:
VAR显示=阵列();
$('。thread_child')。每个(函数(指数值){
显示[指数] =无;
如果($(本)。是(:可见)){
显示[指数] =块;
}
});
我尝试把它变成一个JSON对象:
数据= JSON.stringify(显示器);
这似乎并没有发送正确的JSON格式!
如果我的手code像这样,它的工作原理和信息发送:
数据= {\"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对象上做一个警告,它看起来一样的手codeD之一。但是,这是行不通的。
我要疯了试图解决这个!我缺少的是在这里吗?什么是发送此信息拿到手codeD格式的最佳方式?
我使用这种方法的AJAX发送数据:
$。阿贾克斯({
数据类型:JSON
数据:数据,
网址:myfile.php
缓存:假的,
方法:GET,
成功:函数(RSP){
警报(JSON.stringify(RSP));
VAR内容= RSP;
VAR模板=渲染('tsk_lst');
VAR HTML模板=({内容:内容});
$(#task_lists)。html的(HTML);
}
});
使用GET方法,因为我在显示信息(不更新或插入)。只有发送显示信息,以我的PHP文件。
端到端的解决方案
VAR显示器= {};
$('。thread_child')。每个(函数(指数值){
显示[指数] =无;
如果($(本)。是(:可见)){
显示[指数] =块;
}
});$阿贾克斯({
数据类型:JSON
数据:显示,
网址:myfile.php
缓存:假的,
方法:GET,
成功:函数(RSP){
警报(JSON.stringify(RSP));
VAR内容= RSP;
VAR模板=渲染('tsk_lst');
VAR HTML模板=({内容:内容});
$(#task_lists)。html的(HTML);
}
});
我不能完全肯定,但我认为你是在阵列如何在JSON序列可能惊讶。让我们找出问题所在。考虑以下code:
VAR显示=阵列();
显示[0] =无;
显示[1] =块;
显示[2] =无;的console.log(JSON.stringify(显示));
这会打印:
[无,块,无]
这是JSON如何实际序列化数组。但是你要看到的是这样的:
{0:无,1:块,2:无}
要得到你想要的序列化对象,而不是数组这种格式。因此,让我们重写上面code是这样的:
VAR显示2 = {};
显示2 [0] =无;
显示2 [1] =块;
显示2 [2] =无;的console.log(JSON.stringify(显示2));
这将打印你想要的格式。
您可以玩弄这个位置:<一href=\"http://jsbin.com/oDuhINAG/1/edit?js,console\">http://jsbin.com/oDuhINAG/1/edit?js,console
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!
This is my array loop:
var display = Array();
$('.thread_child').each(function(index, value){
display[index]="none";
if($(this).is(":visible")){
display[index]="block";
}
});
I try to turn it into a JSON object by:
data = JSON.stringify(display);
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"};
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?
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 );
}
});
Using GET method because I'm displaying information (not updating or inserting). Only sending display info to my php file.
END SOLUTION
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 );
}
});
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) );
This will print:
["none","block","none"]
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.
You can play around with this here: http://jsbin.com/oDuhINAG/1/edit?js,console
这篇关于转换的JavaScript对象或数组JSON阿贾克斯数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!