Friday, May 12, 2017

JSON Parse

JSON is often used to exchange data to/from a web server. The data is always a string when you receive it from a web server. Parse the data with JSON.parse() to make it becomes a Javascript object. This site will make it easier for you if you want to get JSON formatter

PARSING JSON - EXAMPLE

Imagine we received this text from a web server:
'{ "name":"John", "age":30, "city":"New York"}'
Use the JavaScript function JSON.parse() in order to convert text into a JavaScript object:
var obj = JSON.parse('{ "name":"John", "age":30, "city":"New York"}');
Reember to make sure that the text is written in JSON format, otherwise, you will receive  a syntax error.
Use the JavaScript object in your page:

Example

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = obj.name + ", " + obj.age
</script>

No comments:

Post a Comment