JSON Stringify从对象中删除数据 [英] JSON Stringify Removing Data From Object

查看:94
本文介绍了JSON Stringify从对象中删除数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力通过jQuery ajax将JSON发送到Node服务器。我的jQuery ajax正在运行。见下文。

I am working on sending JSON, via jQuery ajax, to a Node server. My jQuery ajax is working. See below.

var user = JSON.stringify(userObject);
$.ajax({
        type: 'POST',
        url: 'http://localhost:5000/save',
        contentType: 'application/json',
        dataType: 'json',
        data: user
    })
    .done(function(data) {
        console.log(data.link, 'here is the link');
        callback(data.link);
    })
    .fail(function(err) {
        console.log(err);
        callback(err);
    });

我的问题是,当我控制日志用户,一个已经字符串化的json对象时,我有信息内部数组正在丢失。我失去的对象部分看起来像这样。

My issue is that when I console log user, a json object that has been stringified, I have information inside arrays that are being lost. The part of the object I am losing looks like this.

并且它在控制台中显示为字符串化,如下所示:

And it is showing up stringified in the console like this:

存储在这些内容中的信息不存储父用户对象内的数组。任何关于为什么会这样做的建议都会有所帮助。如果你有任何替代方法可以用来通过jQuery ajax发送这个数据结构,请告诉我。

The information that is stored inside of those arrays inside of the parent user object are not being stored. Any suggestion to why this might be will help. If you have any alternative methods that I can use to send this data structure via jQuery ajax, please let me know.

编辑
以下是创建区域的位置:

Edit Here is where regions is created:

// Add regions to the bracket layout object
        user.regions = [];
        for (var a = 0; a < 2; a++) {
            user.regions[a] = [];
            user.regions[a].columns = [];
        }

谢谢,

推荐答案

嗯,问题是你正在创建 AN ARRAY 然后继续使用它与对象一样。

Well, the problem is that you're creating AN ARRAY then continue working with it as with an object.

使用

user.regions[a] = {};

而不是。

会发生什么? JSON.stringify 看到有一个数组,尝试迭代它没有的数字索引,因此导致一个空数组。

What happens is that JSON.stringify sees there is an array, tries to iterate over its numeric indexes which it does not have so it results in an empty array.

关于JSFiddle的示例: http://jsfiddle.net/Le80jdsj/

Example on JSFiddle: http://jsfiddle.net/Le80jdsj/

这篇关于JSON Stringify从对象中删除数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆