所以我用元素信息创建了一个array.我循环遍历所有元素并保存索引.由于某些原因,我无法将此数组转换为json对象!

This is my array loop:

var display = Array();
$('.thread_child').each(function(index, value){
   display[index]="none";
   if($(this).is(":visible")){
      display[index]="block";
   }
});

我try 通过以下方式将其转换为JSON对象:

data = JSON.stringify(display);

它似乎没有发送正确的JSON格式!

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.


最终解决方案


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"]

这就是JSON实际序列化数组的方式.但是,您希望看到的是类似以下内容:

{"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/edit?js,console

Json相关问答推荐

如何在对象投影(*)上应用滤镜投影([?port==`eth1`])?

Bash和echo命令出现意外结果

如何创建可由Gin序列化到json的排序键值映射?

基于 JSON 字段的 Jolt 条件标志

使用 jq 获取所有嵌套键和值

Powershell解析JSON文件中的键或值

条件性构建/修改嵌套对象数组

将boost::beast::multibuffer转换为std::istream

SwiftUI:如何使用 0 索引数组键为 JSON 添加类型

字典和对象的模型创建问题

ASP.NET MVC - 将 Json 结果与 ViewResult 结合起来

如何一次加载无限滚动中的所有条目以解析python中的HTML

字符串的 Gson 数组到 JsonArray

将 JSON 对象推送到 localStorage 中的数组

Jsonpath 与 Jackson 或 Gson

在 Webpack 中加载静态 JSON 文件

杰克逊:反序列化 for each 值都具有正确类型的 Map

case 类只有一个字段时如何将json转为 case 类

如何从 github API 解析链接头

如何在本地存储中存储对象数组?