Chr*_*son 32 javascript arrays json stringify
所以我正在创建一个包含元素信息的数组.我遍历所有元素并保存索引.出于某种原因,我无法将此数组转换为json对象!
这是我的数组循环:
var display = Array();
$('.thread_child').each(function(index, value){
display[index]="none";
if($(this).is(":visible")){
display[index]="block";
}
});
Run Code Online (Sandbox Code Playgroud)
我尝试通过以下方式将其转换为JSON对象:
data = JSON.stringify(display);
Run Code Online (Sandbox Code Playgroud)
它似乎没有发送正确的JSON格式!
如果我像这样手动编码,它可以工作并发送信息:
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"};
Run Code Online (Sandbox Code Playgroud)
当我对JSON.stringify对象发出警报时,它看起来与手动编码的对象相同.但它不起作用.
我疯了试图解决这个问题!我在这里错过了什么?发送此信息以获得手动编码格式的最佳方法是什么?
我使用这个ajax方法发送数据:
$.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 );
}
});
Run Code Online (Sandbox Code Playgroud)
使用GET方法,因为我正在显示信息(不更新或插入).只将显示信息发送到我的php文件.
结束解决方案
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 );
}
});
Run Code Online (Sandbox Code Playgroud)
Shi*_*hah 70
我不完全确定,但我认为你可能会对如何在JSON中序列化数组感到惊讶.让我们找出问题所在.考虑以下代码:
var display = Array();
display[0] = "none";
display[1] = "block";
display[2] = "none";
console.log( JSON.stringify(display) );
Run Code Online (Sandbox Code Playgroud)
这将打印:
["none","block","none"]
Run Code Online (Sandbox Code Playgroud)
这就是JSON实际序列化数组的方式.但是你想要看到的是:
{"0":"none","1":"block","2":"none"}
Run Code Online (Sandbox Code Playgroud)
要获得此格式,您需要序列化对象,而不是数组.所以让我们重写上面这样的代码:
var display2 = {};
display2["0"] = "none";
display2["1"] = "block";
display2["2"] = "none";
console.log( JSON.stringify(display2) );
Run Code Online (Sandbox Code Playgroud)
这将以您想要的格式打印.
你可以在这里玩这个:http://jsbin.com/oDuhINAG/1/edit?js,console
JVE*_*999 18
你可以使用JSON.stringify(object)
一个对象,我只是编写了一个函数,它将递归地将一个数组转换为一个对象,就像这样JSON.stringify(convArrToObj(array))
,这是下面的代码(更多细节可以在这个答案中找到):
// Convert array to object
var convArrToObj = function(array){
var thisEleObj = new Object();
if(typeof array == "object"){
for(var i in array){
var thisEle = convArrToObj(array[i]);
thisEleObj[i] = thisEle;
}
}else {
thisEleObj = array;
}
return thisEleObj;
}
Run Code Online (Sandbox Code Playgroud)
为了使它更通用,您可以覆盖该JSON.stringify
功能,您不必再担心它,为此,只需将其粘贴到页面顶部:
// Modify JSON.stringify to allow recursive and single-level arrays
(function(){
// Convert array to object
var convArrToObj = function(array){
var thisEleObj = new Object();
if(typeof array == "object"){
for(var i in array){
var thisEle = convArrToObj(array[i]);
thisEleObj[i] = thisEle;
}
}else {
thisEleObj = array;
}
return thisEleObj;
};
var oldJSONStringify = JSON.stringify;
JSON.stringify = function(input){
return oldJSONStringify(convArrToObj(input));
};
})();
Run Code Online (Sandbox Code Playgroud)
现在JSON.stringify
将接受arrays
或objects
!(以示例链接到jsFiddle)
编辑:
这是另一个更高效的版本,虽然它可能会或可能不那么可靠(不确定 - 它取决于是否JSON.stringify(array)
总是返回[]
,我不明白为什么它不会,所以这个功能应该是更好,因为当你使用JSON.stringify
a object
)它做的工作少一点:
(function(){
// Convert array to object
var convArrToObj = function(array){
var thisEleObj = new Object();
if(typeof array == "object"){
for(var i in array){
var thisEle = convArrToObj(array[i]);
thisEleObj[i] = thisEle;
}
}else {
thisEleObj = array;
}
return thisEleObj;
};
var oldJSONStringify = JSON.stringify;
JSON.stringify = function(input){
if(oldJSONStringify(input) == '[]')
return oldJSONStringify(convArrToObj(input));
else
return oldJSONStringify(input);
};
})();
Run Code Online (Sandbox Code Playgroud)