开发者

JSON.stringify returning []

开发者 https://www.devze.com 2023-04-01 06:27 出处:网络
Why would JSON.stringify() return: [] The same happens with jQuery: $.JSON.encode() What could cause this? I am passing in a simple array of objects, where each object has string properties. I hav

Why would JSON.stringify() return:

[]

The same happens with 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开发者_JAVA技巧 methods are returning [].


IN your array declaration

var count_review= new Array() 

instead of this use

var count_review={}; 

It Works!


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

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

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

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


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.

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 Array sense -- 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.org website where it says (some of emphasis below is mine):

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 list of 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 object is an unordered set 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.stringify returning []

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.stringify returning []

The JSON.stringify function 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 MDN as of today (2017-09-04) so here is some low-hanging fruit if you want to help the ol' Mozilla.


I came accros same problem because my json array key-value was string-string. It is not possible to change the structure. So i solve my problem by converting my json array to json object and it solves my problem completely. Json object successfully stringified.

To Change array to object :

var *your_object* = Object.assign({}, *your_array*);
0

精彩评论

暂无评论...
验证码 换一张
取 消