Javascript's JSON.stringify doesn't take k

2019-02-24 23:50发布

In JavaScript, you can have objects, like this:

var a = { foo: 12, bar: 34 };

Or arrays with key (named) indexes, like this:

var b = [];
b['foo'] = 56;
b['bar'] = 78;

They're somewhat similar, but obviously not the same.

Now the strange thing is, JSON.stringify doesn't seem to take the array. No errors or anything, JSON.stringify(b) just results in [].

See this jsfiddle example. Am I doing something wrong, or do I just misunderstand how arrays work?

3条回答
我欲成王,谁敢阻挡
2楼-- · 2019-02-25 00:36

Javascript doesn't support Associative arrays (Like PHP).

var b = []; Declaring explicitly an array, when you are trying to create an Object.

Arrays in Javascript can only contain the Index approach of Arrays, while Objects are more of Associative arrays.

If you change var b = []; to var b = {}; it will solve the problem.

var b = {} Declaring explicitly an Object.

查看更多
相关推荐>>
3楼-- · 2019-02-25 00:41

Javascript arrays are objects. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Predefined_Core_Objects#Array_Object for details.

Note: if you supply a non-integer value to the array operator in the code above, a property will be created in the object representing the array, instead of an array element.

JSON supports only a subset of Javascript. See http://www.json.org/ for details.

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.

A Javascript array that has properties created in the underlying object does not fit into either of these structures because it has both a collection of name/value pairs and an ordered list of values, so there is no simple way to represent such an object directly in JSON.

The JSON.stringify method is defined in the ECMAScript specification. For example, see http://www.ecma-international.org/ecma-262/5.1/#sec-15.12.3.

While there are many details, the bit that is relevant here is how object values are stringified:

If Type(value) is Object, and IsCallable(value) is false

  • If the [[Class]] internal property of value is "Array" then Return the result of calling the abstract operation JA with argument value.
  • Else, return the result of calling the abstract operation JO with argument value.

Given your array, despite the addition of parameters to the underlying object, the result is of stringifying the ordered set of array elements, not the underlying object.

There is nothing wrong about adding parameters to an array object, but they are not part of the array and functions or methods that handle arrays might ignore them or deal with them arbitrarily. You have seen that JSON.stringify ignores the additional parameters. Other functions might do otherwise - you will have to find out in each case.

While it is not wrong, it will probably be easier to understand if you do not add properties to array objects. If you want to add properties, start with a non-array object.

Rather than:

var b = [];
b['foo'] = 56;
b['bar'] = 78;

You might use:

var b = {};
b['foo'] = 56;
b['bar'] = 78;
查看更多
【Aperson】
4楼-- · 2019-02-25 00:43

enter image description here

This snap is from IE explorer. See the array is still blank.

Actually the way of inserting the elements to the array is :

1. Use push() 2. insert the elements in the array during declaration

If you want to stringify the array you have to have the data inside the array.

So, now you want to stringify the key value pairs so you have to pass the object as the argument of JSON.stringify() as follows:

var test = {};           // Object
test['a'] = 'test';
test['b'] = [];          // Array
test['b'].push('item');
test['b'].push('item2');
test['b'].push('item3');
var json = JSON.stringify(test);
alert(json);

Solution to your problem now:

enter image description here

Note: Console of Google Chrome is giving different result, which is a bug in Google Chrome.

查看更多
登录 后发表回答