JSON Stringify
Next ❯JSON PHP
- Well, When you have to send an object to the server, it must be in string
So to convert javaScript object into JSON string we requireJSON.stringify()
- Here, Object can have only JSON data types, otherwise it will skip keys having value other than JSON data types
- Remember! You can also convert an array via this method into a string and can send it to the server
Syntax
JSON.stringify(object)
Example
var obj = {"name":"Riya","age":23}; // Object
Convert into a StringBefore : [object Object]
After Stringify : {"name":"Riya","age":23}
var obj = {"name":"Riya","age":23};
var jsonString = JSON.stringify(obj);
$("p").html("Before : "+obj);
$("p").append("<br>After Stringify : "+ jsonString);
JSON.stringifysubject
JSON.stringifyclose
<!DOCTYPE html>
<html>
<head>
<title>Full Code</title>
</head>
<body>
<p style="text-align:center;"></p>
<script>
var obj = {"name":"Riya","age":23};
var jsonString = JSON.stringify(obj);
$("p").html("Before : "+obj);
$("p").append("<br>After Stringify : "+ jsonString);
</script>
</body>
</html>
❮ Prev Arrays
Next ❯JSON PHP