Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON.stringify returning []

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

like image 253
Chris Avatar asked Aug 31 '11 15:08

Chris


People also ask

What is the return type of JSON Stringify?

It should return the value that should be added to the JSON string, as follows: If you return a Number , String , Boolean , or null , the stringified version of that value is used as the property's value. If you return a Function , Symbol , or undefined , the property is not included in the output.

Can you use JSON Stringify on an array?

The JSON array data type cannot have named keys on an array. When you pass a JavaScript array to JSON. stringify the named properties will be ignored. If you want named properties, use an Object, not an Array.

Is it bad to use JSON Stringify?

It`s ok to use it with some primitives like Numbers, Strings or Booleans. As you can see, you can just lose unsupported some data when copying your object in such a way. Moreover, JavaScript won`t even warn you about that, because calling JSON. stringify() with such data types does not throw any error.

What is the purpose of JSON Stringify () and parse () methods?

The JSON. parse() function is used to convert a string into a JavaScript object while the JSON. stringify() function is used to convert a JavaScript object into a string.


1 Answers

IN your array declaration

var count_review= new Array()  

instead of this use

var count_review={};  

It Works!

like image 189
amol challawar Avatar answered Sep 21 '22 17:09

amol challawar