Javascript JSON.stringify 对象包含数组序列化问题
在 javascript 中,我有一个看起来类似于以下内容的对象:
var myObj = {
prop1: 1,
prop2: 2,
prop3: ["a", "b", "c", "d", "e"],
prop4: 4,
prop5: ["f", "g", "h", "i"]
}
它是一个包含许多属性的对象。每个属性可能是也可能不是数组。
var serializedMyObj = JSON.stringify(myObj);
serializedMyObj
是(通过查看 firebug 中序列化函数的结果找到的):
"{ "prop1":1, "prop2":2, "prop3":["a","b","c","d", "e"], "prop4":4, "prop5":["f","g","h","i"] }"
如果我 alert(serializedMyobj);
它向我显示:
{
"prop1": 1,
"prop2": 2,
"prop3": [],
"prop4": 4,
"prop5": []
}
真正的问题是当我传递此数据时到 Asp.Net PageMethod 中,服务器获得的数据与我在警报对话框中显示的数据相同,而不是在 firebug 中显示的数据。在某个地方它丢失了数组值并且只放入 []
。
有谁知道为什么会发生这种情况或解决方法?这可能是我忽略的简单事情。
I javascript I have an object that looks similar to:
var myObj = {
prop1: 1,
prop2: 2,
prop3: ["a", "b", "c", "d", "e"],
prop4: 4,
prop5: ["f", "g", "h", "i"]
}
It's an object containing a number of properties. Each property may or may not be an array.
var serializedMyObj = JSON.stringify(myObj);
serializedMyObj
is (found by viewing the results of the serialize function in firebug):
"{ "prop1":1, "prop2":2, "prop3":["a","b","c","d", "e"], "prop4":4, "prop5":["f","g","h","i"] }"
if I alert(serializedMyobj);
it shows me:
{
"prop1": 1,
"prop2": 2,
"prop3": [],
"prop4": 4,
"prop5": []
}
The real problem is when i pass this data into an Asp.Net PageMethod the server gets the same data I see when it's shown in the alert dialog, not in firebug. Somewhere it's losing the array values and only putting in []
.
Does anyone know why this would happen or a way to fix it? It's probably something simple I'm overlooking.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在 Firefox 上得到以下(正确的)输出:
您使用的是什么浏览器?
另外,我注意到
myObj
在JSON.stringify(myobj);
中是小写的 - 我认为这只是一个拼写错误?I get the following (correct) output on firefox:
What browser are you using?
Also, I noticed that
myObj
was lowercase inJSON.stringify(myobj);
- I assume that was just a typo?