JSON.stringify()


فهرست مطالب

    نمایش فهرست مطالب


.ﺖﺳﺍ ﺭﻭﺮﺳ ﺏﻭ ﮏﯾ ﺯﺍ/ﻪﺑ ﻩﺩﺍﺩ ﻝﺩﺎﺒﺗ ،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>

.ﺪﯿﻨﮐ ﻞﯾﺪﺒﺗ ﻩﺪﻧﺮﯿﮔ ﺭﺩ ﺦﯾﺭﺎﺗ ﯽﺷ ﮏﯾ ﻪﺑ ﻩﺭﺎﺑﻭﺩ ﺍﺭ ﻪﺘﺷﺭ ﺪﯿﻧﺍﻮﺗ ﯽﻣ ﺎﻤﺷ


Stringify ﻊﺑﺍﻮﺗ

.ﺪﻨﺘﺴﯿﻧ ﺯﺎﺠﻣ ﯽﺷ ﺮﯾﺩﺎﻘﻣ ﻥﺍﻮﻨﻋ ﻪﺑ ﻊﺑﺍﻮﺗ ،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>

ﺩﺍﺩ ﺪﻨﻫﺍﻮﺧ ﺖﺳﺩ ﺯﺍ ﺍﺭ ﻩﺪﻧﺮﯿﮔ ﻭ ﺩﻮﺧ ﻪﻨﻣﺍﺩ ﻊﺑﺍﻮﺗ ،ﺪﯿﻨﮐ ﻝﺎﺳﺭﺍ JSON ﺯﺍ ﻩﺩﺎﻔﺘﺳﺍ ﺎﺑ ﺍﺭ ﻊﺑﺍﻮﺗ