JSON Parse
Next ❯Objects
- Well, Every data came from a server side is in string
So to convert JSON string into JavaScript object we requireJSON.parse()
- Remember! You can also convert string like array via this method into an array
Syntax
JSON.parse(jsonString)
Example
var jsonString = '{"name":"Riya","age":23}'; // String
Convert intovar obj = {"name":"Riya","age":23}; //Object
var jsonString = '{"name":"Riya","age":23}';
var obj = JSON.parse(jsonString);
$("p").html("Before : "+jsonString);
$("p").append("<br>After Parse : "+ obj);
JSON.parsesubject
JSON.parseclose
<!DOCTYPE html>
<html>
<head>
<title>Full Code</title>
<!--Required jQuery Library file-->
<script src="jquery.min.js"></script>
</head>
<body>
<p style="text-align:center;"></p>
<script>
var jsonString = '{"name":"Riya","age":23}';
var obj = JSON.parse(jsonString);
$("p").html("Before : "+jsonString);
$("p").append("<br>After Parse : "+ obj);
</script>
</body>
</html>
- To convert your object back to string, we have
JSON.stringify()
method, you will see it in coming section - To access object data , see next section
❮ Prev Ways To Add
Next ❯Objects