Tuesday, 15 August 2017

What is Use Case For JSON Parse and JSON Stringify - W3tricks

What is Use Case For JSON Parse and JSON Stringify


index.html


<html>
<head>
<title>W3tricks</title>
</head>
<body>
<div id="output"></div>
<input type="text" id="myvalue" value="Obama"/>
<script src="script.js"></script>
</body>
</html>

Script.js


var student = '{"student1":{"name" : "Bob", "age" : "18", "city" : "new york"}, "student2": {"name" : "Alice", "age" : "16", "city" : "paris"}}';

var MyValue = document.getElementById('myvalue');
var MyObj = JSON.parse(student);
var sendAjax = JSON.stringify(MyObj);

MyValue.onchange = function(){
console.log(MyObj);
MyObj.student1.name = this.value;
sendAjax = JSON.stringify(MyObj);
document.getElementById('output').innerHTML = sendAjax;
}

Output - 1


Output - 2

0 comments:

Post a Comment