.ﺖﺳﺍ ﺭﻭﺮﺳ ﺏﻭ ﮏﯾ ﺯﺍ/ﻪﺑ ﻩﺩﺍﺩ ﻝﺩﺎﺒﺗ ،JSON ﺞﯾﺍﺭ ﯼﺎﻫﺩﺮﺑﺭﺎﮐ ﺯﺍ ﯽﮑﯾ
ﺪﻨﺷﺎﺑ ﺪﯾﺎﺑ ﺎﻫ ﻩﺩﺍﺩ ،ﺏﻭ ﺭﻭﺮﺳ ﻪﺑ ﻩﺩﺍﺩ ﻝﺎﺳﺭﺍ ﻡﺎﮕﻨﻫ
.ﺪﯿﻨﮐ ﻞﯾﺪﺒﺗ ﻪﺘﺷﺭ ﻪﺑ JSON.stringify()
ﺎﺑ ﺍﺭ ﺖﭙﯾﺮﮑﺳﺍ ﺍ
:ﻢﯾﺭﺍﺩ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﺭﺩ ﺍﺭ ﯽﺷ ﻦﯾﺍ ﺪﯿﻨﮐ ﺭﻮﺼﺗ
const obj = {name: "John", age: 30, city: "New York"};
.ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ ﻪﺘﺷﺭ ﻪﺑ ﻥﺁ ﻞﯾﺪﺒﺗ ﯼﺍﺮﺑ JSON.stringify()
const myJSON = JSON.stringify(obj);
.ﺩﻮﺑ ﺪﻫﺍﻮﺧ JSON ﺩﺎﻤﻧ ﻝﺎﺒﻧﺩ ﻪﺑ ﻪﺘﺷﺭ ﮏﯾ ﻪﺠﯿﺘﻧ
myJSON
ﺖﺳﺍ ﺭﻭﺮﺳ ﻪﺑ ﻝﺎﺳﺭﺍ ﻩﺩﺎﻣﺁ ﻭ ﺖﺳﺍ ﻪﺘﺷﺭ ﮏﯾ ﻥﻮﻨﮐﺍ:
const obj = {name: "John", age: 30, city: "New York"};
const myJSON =
JSON.stringify(obj);
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>Create a JSON string from a JavaScript object.</h2>
<p id="demo"></p>
<script>
const obj = {name: "John", age: 30, city: "New York"};
const myJSON = JSON.stringify(obj);
document.getElementById("demo").innerHTML = myJSON;
</script>
</body>
</html>
.ﺖﺧﻮﻣﺁ ﺪﯿﻫﺍﻮﺧ ﯼﺪﻌﺑ ﯼﺎﻫ ﻞﺼﻓ ﺭﺩ ﺍﺭ ﺭﻭﺮﺳ ﻪﺑ JSON ﻝﺎﺳﺭﺍ ﻩﻮﺤﻧ
:ﺩﺮﮐ ﯼﺪﻨﺑﻪﺘﺷﺭ ﺍﺭ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﯼﺎﻫﻪﯾﺍﺭﺁ ﻥﺍﻮﺗﯽﻣ ﻦﯿﻨﭽﻤﻫ
:ﻢﯾﺭﺍﺩ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﺭﺩ ﺍﺭ ﻪﯾﺍﺭﺁ ﻦﯾﺍ ﺪﯿﻨﮐ ﺭﻮﺼﺗ
const arr = ["John", "Peter", "Sally", "Jane"];
.ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ ﻪﺘﺷﺭ ﻪﺑ ﻥﺁ ﻞﯾﺪﺒﺗ ﯼﺍﺮﺑ JSON.stringify()
const myJSON = JSON.stringify(arr);
.ﺩﻮﺑ ﺪﻫﺍﻮﺧ JSON ﺩﺎﻤﻧ ﻝﺎﺒﻧﺩ ﻪﺑ ﻪﺘﺷﺭ ﮏﯾ ﻪﺠﯿﺘﻧ
myJSON
ﺖﺳﺍ ﺭﻭﺮﺳ ﻪﺑ ﻝﺎﺳﺭﺍ ﻩﺩﺎﻣﺁ ﻭ ﺖﺳﺍ ﻪﺘﺷﺭ ﮏﯾ ﻥﻮﻨﮐﺍ:
const arr = ["John", "Peter", "Sally", "Jane"];
const myJSON =
JSON.stringify(arr);
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>Create a JSON string from a JavaScript array.</h2>
<p id="demo"></p>
<script>
const arr = ["John", "Peter", "Sally", "Jane"];
const myJSON = JSON.stringify(arr);
document.getElementById("demo").innerHTML = myJSON;
</script>
</body>
</html>
.ﺖﺧﻮﻣﺁ ﺪﯿﻫﺍﻮﺧ ﯼﺪﻌﺑ ﯼﺎﻫ ﻞﺼﻓ ﺭﺩ ﺍﺭ ﺭﻭﺮﺳ ﻪﺑ JSON ﻪﺘﺷﺭ ﻝﺎﺳﺭﺍ ﻩﻮﺤﻧ
،ﺪﯿﻨﮐ ﯽﻣ ﻩﺮﯿﺧﺫ ﺍﺭ ﻥﺁ ﺎﺠﮐ ﻪﮑﻨﯾﺍ ﺯﺍ ﺮﻈﻧ ﻑﺮﺻ ﻭ ﺪﻨﺷﺎﺑ ﻪﺘﺷﺍﺩ ﯽﺻﺎﺧ ﺖﻣﺮﻓ ﺪﯾﺎﺑ ﺎﻫ ﻩﺩﺍﺩ ،ﺎﻫ ﻩﺩ
JSON ﺪﻨﮐ ﯽﻣ ﻢﻫﺍﺮﻓ ﻦﺘﻣ ﻥﺍﻮﻨﻋ ﻪﺑ ﺍﺭ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ءﺎﯿﺷﺍ ﻩﺮﯿﺧﺫ ﻥﺎﮑﻣﺍ.
ﯽﻠﺤﻣ ﻪﻈﻓﺎﺣ ﺭﺩ ﺎﻫ ﻩﺩﺍﺩ ﯼﺯﺎﺳ ﻩﺮﯿﺧﺫ
// Storing data:
const myObj = {name: "John",
age: 31, city: "New York"};
const myJSON =
JSON.stringify(myObj);
localStorage.setItem("testJSON", myJSON);
// Retrieving data:
let text = localStorage.getItem("testJSON");
let obj =
JSON.parse(text);
document.getElementById("demo").innerHTML = obj.name;
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>Store and retrieve data from local storage.</h2>
<p id="demo"></p>
<script>
// Storing data:
const myObj = { name: "John", age: 31, city: "New York" };
const myJSON = JSON.stringify(myObj);
localStorage.setItem("testJSON", myJSON);
// Retrieving data:
let text = localStorage.getItem("testJSON");
let obj = JSON.parse(text);
document.getElementById("demo").innerHTML = obj.name;
</script>
</body>
</html>
ﺪﺷ ﺪﻫﺍﻮﺧ ﻞﯾﺪﺒﺗ JSON.stringify()
ﻊﺑﺎﺗ .ﺪﻨﺘﺴﯿﻧ ﺯﺎﺠﻣ ﺦﯾ
const obj = {name: "John", today: new Date(), city : "New York"};
const myJSON = JSON.stringify(obj);
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JSON.stringify() converts date objects into strings.</h2>
<p id="demo"></p>
<script>
const obj = {name: "John", today: new Date(), city: "New York"};
const myJSON = JSON.stringify(obj);
document.getElementById("demo").innerHTML = myJSON;
</script>
</body>
</html>
.ﺪﯿﻨﮐ ﻞﯾﺪﺒﺗ ﻩﺪﻧﺮﯿﮔ ﺭﺩ ﺦﯾﺭﺎﺗ ﯽﺷ ﮏﯾ ﻪﺑ ﻩﺭﺎﺑﻭﺩ ﺍﺭ ﻪﺘﺷﺭ ﺪﯿﻧﺍﻮﺗ ﯽﻣ ﺎﻤﺷ
.ﺪﻨﺘﺴﯿﻧ ﺯﺎﺠﻣ ﯽﺷ ﺮﯾﺩﺎﻘﻣ ﻥﺍﻮﻨﻋ ﻪﺑ ﻊﺑﺍﻮﺗ ،JSON ﺭﺩ
ﺪﻨﮐ ﯽﻣ ﻑﺬﺣ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﺯﺍ ﺍﺭ ﯽﻌﺑﺎﺗ ﺮﻫ → ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ ﺩﺮﮐ ﻑﺬﺣ ﺍﺭ ﻥﺁ ﻥﺍﻮﺗﯽﻣ ،ﺪﯿﻨﮐ ﻞﯾﺪﺒﺗ ﻪﺘﺷﺭ ﻪﺑ ﺍﺭ ﺩﻮﺧ ﻊﺑﺍﻮﺗ ،ﺍﺮﺟﺍ ﺯﺍ ﻞﺒﻗ ﺮﮔﺍ → ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ ﺩﺍﺩ ﺪﻨﻫﺍﻮﺧ ﺖﺳﺩ ﺯﺍ ﺍﺭ ﻩﺪﻧﺮﯿﮔ ﻭ ﺩﻮﺧ ﻪﻨﻣﺍﺩ ﻊﺑﺍﻮﺗ ،ﺪﯿﻨﮐ ﻝﺎﺳﺭﺍ JSON ﺯﺍ ﻩﺩﺎﻔﺘﺳﺍ ﺎﺑ ﺍﺭ ﻊﺑﺍﻮﺗJSON.stringify()
ﻝﺎﺜﻣ
const obj = {name: "John", age: function () {return 30;}, city: "New York"};
const myJSON = JSON.stringify(obj);
<!DOCTYPE html>
<html>
<body>
<h2>JSON.stringify() will remove any functions from an object.</h2>
<p id="demo"></p>
<script>
const obj = {name: "John", age: function () {return 30;}, city: "New York"};
const myJSON = JSON.stringify(obj);
document.getElementById("demo").innerHTML = myJSON;
</script>
</body>
</html>
ﻝﺎﺜﻣ
const obj = {name: "John", age: function () {return 30;}, city: "New York"};
obj.age = obj.age.toString();
const myJSON = JSON.stringify(obj);
<!DOCTYPE html>
<html>
<body>
<h2>JSON.stringify() will remove any functions from an object.</h2>
<p>Convert functions into strings to keep them in the JSON object.</p>
<p id="demo"></p>
<script>
const obj = {name: "John", age: function () {return 30;}, city: "New York"};
obj.age = obj.age.toString();
const myJSON = JSON.stringify(obj);
document.getElementById("demo").innerHTML = myJSON;
</script>
</body>
</html>