Javascript JSON.stringify 返回 []

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7259728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:31:07  来源:igfitidea点击:

JSON.stringify returning []

javascriptjqueryjson

提问by Chris

Why would JSON.stringify()return:

为什么会JSON.stringify()回来:

[]

The same happens with jQuery: $.JSON.encode()

jQuery 也会发生同样的情况: $.JSON.encode()

What could cause this? I am passing in a simple array of objects, where each object has string properties. I have looped through and alerted each objects properties and all looks fine, but for some reason both the encode methods are returning [].

什么可能导致这种情况?我传入了一个简单的对象数组,其中每个对象都有字符串属性。我已经遍历并提醒每个对象的属性,一切看起来都很好,但由于某种原因,两个编码方法都返回[].

回答by amol challawar

IN your array declaration

在您的数组声明中

var count_review= new Array() 

instead of this use

而不是这种用途

var count_review={}; 

It Works!

有用!

回答by Ehsan Jelodar

If your array uses a stringkey value, instead of an int, stringify ignores that value.

如果您的数组使用string键值而不是int,则 stringify 将忽略该值。

var array = [];
array['name'] = 'johan';
array['age'] = 20;
alert(JSON.stringify(array))// return []

But if array uses an "int" as a key, then stringifywill return it.

但是如果数组使用“int”作为键,stringify则将返回它。

var array = [];
array[0] = 'johan';
array[1] = 20;
alert(JSON.stringify(array))// return["johan",20]

回答by Bojan Markovic

While the answers above are true, to fully understand it definitely needs more context.

虽然上面的答案是正确的,但要完全理解它肯定需要更多的上下文。

JSON is an universal format (in the sense of supporting multiple, hopefully all programming languages) that is not entirely a 1-to-1 mapping of JavaScript objects.

JSON 是一种通用格式(在支持多种编程语言的意义上),它不完全是 JavaScript 对象的一对一映射。

So the root reason for why this happens is that in JSON (as specified by RFC 7159) there are no associative arrays in the JavaScript's Arraysense -- i.e. arrays whose keys are strings. As reading RFC specs can be rather time-consuming, it can be most easily seen on top of the JSON.orgwebsite where it says (some of emphasis below is mine):

因此,发生这种情况的根本原因是在 JSON(如RFC 7159所指定)中没有 JavaScriptArray意义上的关联数组——即键为字符串的数组。由于阅读 RFC 规范可能相当耗时,因此最容易在JSON.org网站顶部看到它说(下面的一些重点是我的):

JSON is built on two structures:

  • A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
  • An ordered listof values. In most languages, this is realized as an array, vector, list, or sequence.

These are universal data structures. Virtually all modern programming languages support them in one form or another. It makes sense that a data format that is interchangeable with programming languages also be based on these structures.

In JSON, they take on these forms:

An objectis an unorderedset of name/value pairs. An object begins with {(left brace) and ends with }(right brace). Each name is followed by :(colon) and the name/value pairs are separated by ,(comma).

JSON Object definition graph

An array is an ordered collection of values. An array begins with [(left bracket) and ends with ](right bracket). Values are separated by ,(comma).

JSON Array definition graph

JSON 建立在两种结构上:

  • 名称/值对的集合。在各种语言中,这被实现为对象记录结构字典哈希表键控列表关联数组
  • 值的有序列表。在大多数语言中,这被实现为数组向量列表序列

这些是通用数据结构。几乎所有现代编程语言都以一种或另一种形式支持它们。可与编程语言互换的数据格式也基于这些结构是有道理的。

在 JSON 中,它们采用以下形式:

一个对象是一个无序集合的名称/值对。一个对象以{(左大括号)开始,以(右大括号)结束}。每个名称后跟:(冒号),名称/值对由,(逗号)分隔。

JSON 对象定义图

数组是值的有序集合。数组以[(左括号)开始,以(右括号)结束]。值由,(逗号)分隔。

JSON Array 定义图

The JSON.stringifyfunction handles this in such way that assuming that array is intended to be an array, and all keyed entries are annotative, so much like elements that are function objects, they are just thrown out of the JSON representation it returns. Unfortunately this behaviour is not documented in a well defined and easily understandable way in the unofficial JavaScript reference that is MDNas of today (2017-09-04) so here is some low-hanging fruit if you want to help the ol' Mozilla.

JSON.stringify函数以这样的方式处理此问题,即假设该数组旨在成为一个数组,并且所有带键的条目都是注释性的,就像作为函数对象的元素一样,它们只是从它返回的 JSON 表示中抛出。不幸的是,截至今天(2017 年 9 月 4 日),非官方 JavaScript 参考文档MDN 中并未以明确定义且易于理解的方式记录此行为,因此如果您想帮助 ol' Mozilla,这里有一些容易实现的成果。