Is it possible to generate JSON from Javascript?

2

Is there any way to do the JSON encode that is in PHP, but from JavaScript or jQuery?

I need to pass an array that I create from JavaScript through an Ajax connection that I send by POST, and then this request is transformed by PHP into a GET request, that's why I want to have the array in a text string, so as not to have a variable GET each position of the array.

Thanks in advance.

    
asked by gontrollez 21.06.2016 в 13:17
source

1 answer

17

You must use JSON.stringify , this converts any javascript structure (arrays or objects) to its representation in JSON format.

Example:

console.log(
   JSON.stringify([1,2,3,4,5])
);

console.log(
   JSON.stringify({hola: "mundo"})
);

Note: It is not a component of Jquery but it is an API present in the browsers

    
answered by 21.06.2016 / 13:19
source