Serializing to JSON in JavaScript
Topics related to client-side programming language.
Post questions and answers about JavaScript, Ajax, or jQuery codes and scripts.
-
MarPlo
- Posts:186
Serializing to JSON in JavaScript
I need to serialize an object to JSON in JavaScript. Is there a "standard" way to do this?
I have an array defined something like this:
Code: Select all
var countries = [];
countries[0] = 'ga';
countries[1] = 'cd';
...
And I need to convert this into a json string to pass to $.ajax() like this:
Code: Select all
$.ajax({
type: "POST",
url: "Concessions.aspx/GetConcessions",
data: "{'countries': ['ga','cd']}",
...
Admin
Posts:805
Use the JSON object methods.
- To convert an object to a string, use
JSON.stringify():
Code: Select all
var json_string = JSON.stringify(your_object, null, 2);
- To convert a JSON string to object, use
JSON.parse():
Code: Select all
var your_object = JSON.parse(json_string);
Similar Topics
- Display message to every minute in Javascript
JavaScript - jQuery - Ajax
First post
Hello,
On eatch minute from the current hour I wanna have an message
I can not find out how to complete
I hope to get something like this (code...
Last post
If you only want to display a message to every minute, just use the setInterval() function. It calls a function repeatedly, over and over again, at...
- Hour and Minutes togheter in Javascript
JavaScript - jQuery - Ajax
First post
Dear Coursesweb I can not find out how to add the hours + minutes togheter.
<SCRIPT LANGUAGE= JavaScript >
day = new Date()
hr =...
Last post
See and use the following example:
<script>
var day = new Date();
let hr = day.getHours();
let mn = day.getMinutes();
let se =...