JS对象的Stringify添加了额外的数据 [英] Stringify of JS object adds extra data

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

问题描述

在SO上获得了很大的帮助之后,我使用以下代码段构造了一个JS对象:

I have constructed a JS object using the following snippet after getting some great help here on SO:

var noticeMap = $('#preExamNoticesTable tbody tr').map(function() {
    var $cells = $(this).children();
    return {
      sequence: $cells.eq(0).children('input').val(),
      noticeUID: $cells.eq(1).text()
    };
});

产生的示例noticeMap如下所示(来自firebug):

An example resulting noticeMap looks like this (from firebug):

jQuery(Object { sequence="1", noticeUID="JP-L23013663997630352308"},
Object { sequence="3", noticeUID="JP-L22913664089460612172"}, 
Object { sequence="4", noticeUID="JP-L22913664090188631530"}, 
Object { sequence="2", noticeUID="JP-L22913664089408651799"})

当我尝试JSON.stringify(noticeMap)时,它的工作原理非常出色,它为我不想通过POST传递的字符串添加了额外的数据.它为每个值(noticeMap中的对象)生成数字键,但是在最后一个所需的k:v对之后,我的字符串还具有 context obj length prevObject obj.这是我stringify()它和POST之后的JSON:

When I try to JSON.stringify(noticeMap) it works great apart from that it adds extra data to the string that I do not want to pass on with my POST. It generates numerical keys for each value (object from noticeMap), but after the last desired k:v pair my string also has context obj, length, and prevObject obj. Here is the JSON after I stringify() it and POST:

{ "0": { "sequence": "1", "noticeUID": "JP-L23013663997630352308" }, "1": { "sequence": "3", "noticeUID": "JP-L22913664089460612172" }, "2": { "sequence": "4", "noticeUID": "JP-L22913664090188631530" }, "3": { "sequence": "2", "noticeUID": "JP-L22913664089408651799" }, "length": 4, "prevObject": { "0": {}, "1": {}, "2": {}, "3": {}, "length": 4, "prevObject": { "0": { "location": {}, "jQuery19105836315711643562": 1 }, "context": { "location": {}, "jQuery19105836315711643562": 1 }, "length": 1 }, "context": { "location": {}, "jQuery19105836315711643562": 1 }, "selector": "#preExamNoticesTable tbody tr" }, "context": { "location": {}, "jQuery19105836315711643562": 1 } }

我不知道是什么原因导致了这种情况的发生.我开始假设它与字符串处理如何从对象的obj创建JSON字符串有关,但是后来我无法使用现在工作得很漂亮的noticeMaps().有什么想法吗?

I can not figure out what is causing this to happen. I started to hypothesize it had to do with how stringify handles creating a JSON string from obj of objects, but then I can't use my noticeMaps() which is working beautifully now. Any ideas?

谢谢!

推荐答案

您正在序列化jQuery对象,因此其中的所有可序列化属性都包含在输出中.尝试以下方法:

You're serializing a jQuery object, so all serializable properties in it are being included in the output. Try this instead:

JSON.stringify(noticeMap.get()); // .get will return an array

这篇关于JS对象的Stringify添加了额外的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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