Javascript JSON.stringify 对象包含数组序列化问题

发布于 2024-08-22 09:47:00 字数 809 浏览 10 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

话少情深 2024-08-29 09:47:00

我在 Firefox 上得到以下(正确的)输出:

{"prop1":1,"prop2":2,"prop3":["a","b","c","d","e"],"prop4":4,"prop5":["f","g","h","i"]}

您使用的是什么浏览器?

另外,我注意到 myObjJSON.stringify(myobj); 中是小写的 - 我认为这只是一个拼写错误?

I get the following (correct) output on firefox:

{"prop1":1,"prop2":2,"prop3":["a","b","c","d","e"],"prop4":4,"prop5":["f","g","h","i"]}

What browser are you using?

Also, I noticed that myObj was lowercase in JSON.stringify(myobj); - I assume that was just a typo?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文