JSON.parse()


فهرست مطالب

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


.ﺖﺳﺍ ﺭﻭﺮﺳ ﺏﻭ ﮏﯾ ﺯﺍ/ﻪﺑ ﻩﺩﺍﺩ ﻝﺩﺎﺒﺗ ،JSON ﺞﯾﺍﺭ ﯼﺎﻫﺩﺮﺑﺭﺎﮐ ﺯﺍ ﯽﮑﯾ

.ﺪﻨﺘﺴﻫ ﻪﺘﺷﺭ ﮏﯾ ﻪﺸﯿﻤﻫ ﺎﻫ ﻩﺩﺍﺩ ،ﺭﻭﺮﺳ ﺏﻭ ﮏﯾ ﺯﺍ ﻩﺩﺍﺩ ﺖﻓﺎﯾﺭﺩ ﻡﺎﮕﻨﻫ

.ﺪﻧﻮﺷ ﯽﻣ ﻞﯾﺪﺒﺗ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﯽﺷ ﮏﯾ ﻪﺑ ﺎﻫ ﻩﺩﺍﺩ ﻭ ﺪﯿﻨﮐ ﻪﯾﺰﺠﺗ J


JSON ﻪﯾﺰﺠﺗ - ﻝﺎﺜﻣ

:ﻢﯾﺍ ﻩﺩﺮﮐ ﺖﻓﺎﯾﺭﺩ ﺭﻭﺮﺳ ﺏﻭ ﮏﯾ ﺯﺍ ﺍﺭ ﻦﺘﻣ ﻦﯾﺍ ﺪﯿﻨﮐ ﺭﻮﺼﺗ

'{"name":"John", "age":30, "city":"New York"}'

:ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﯽﺷ ﮏﯾ ﻪﺑ ﻦﺘﻣ ﻞﯾﺪﺒﺗ ﯼﺍﺮﺑ JSON.par

const obj = JSON.parse('{"name":"John", "age":30, "city":"New 
  York"}');
  

.ﺪﺷ ﺪﯿﻫﺍﻮﺧ ﻪﺟﺍﻮﻣ ﯼﻮﺤﻧ ﯼﺎﻄﺧ ﮏﯾ ﺎﺑ ﺕﺭﻮﺻ ﻦﯾﺍ ﺮﯿﻏ ﺭﺩ ،ﺪﺷﺎﺑ JSON ﺖﻣﺮﻓ ﺎﺑ ﻦﺘﻣ ﻪﮐ ﺪﯾﻮﺷ ﻦﺌﻤﻄﻣ

:ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ ﺩﻮﺧ ﻪﺤﻔﺻ ﺭﺩ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﯽﺷ ﺯﺍ

ﻝﺎﺜﻣ

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

<script>
document.getElementById("demo").innerHTML = obj.name;
</script>

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>Creating an Object from a JSON String</h2>

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

<script>
const txt = '{"name":"John", "age":30, "city":"New York"}'
const obj = JSON.parse(txt);
document.getElementById("demo").innerHTML = obj.name + ", " + obj.age;
</script>

</body>
</html>

JSON ﻥﺍﻮﻨﻋ ﻪﺑ ﻪﯾﺍﺭﺁ

ﺵﻭﺭ ،ﻪﯾﺍﺭﺁ ﮏﯾ ﺯﺍ ﻩﺪﺷ ﻖﺘﺸﻣ JSON ﺭﺩ JSON.parse() ﺯﺍ ﻩﺩ

ﻝﺎﺜﻣ

const text = '["Ford", "BMW", "Audi", "Fiat"]';
const myArr = JSON.parse(text);

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>Parsing a JSON Array.</h2>
<p>Data written as an JSON array will be parsed into a JavaScript array.</p>
<p id="demo"></p>

<script>
const text = '[ "Ford", "BMW", "Audi", "Fiat" ]';
const myArr = JSON.parse(text);
document.getElementById("demo").innerHTML = myArr[0];
</script>

</body>
</html>


ﺎﻫﺎﻨﺜﺘﺳﺍ

ﺎﻫ ﺦﯾﺭﺎﺗ ﻪﯾﺰﺠﺗ

.ﺪﻨﺘﺴﯿﻧ ﺯﺎﺠﻣ JSON ﺭﺩ ﺦﯾﺭﺎﺗ ءﺎﯿﺷﺍ

.ﺪﯿﺴﯾﻮﻨﺑ ﯼﺍ ﻪﺘﺷﺭ ﺕﺭﻮﺻ ﻪﺑ ﺍﺭ ﻥﺁ ،ﺪﯾﺭﺍﺩ ﺦﯾﺭﺎﺗ ﺮﮐﺫ ﻪﺑ ﺯﺎﯿﻧ ﺮﮔﺍ

:ﺪﯿﻨﮐ ﻞﯾﺪﺒﺗ ﺦﯾﺭﺎﺗ ﯽﺷ ﮏﯾ ﻪﺑ ﻩﺭﺎﺑﻭﺩ ﺍﺭ ﻥﺁ ﺍًﺪﻌﺑ ﺪﯿﻧﺍﻮﺗﯽﻣ

ﻝﺎﺜﻣ

:ﺦﯾﺭﺎﺗ ﻪﺑ ﻪﺘﺷﺭ ﻞﯾﺪﺒﺗ

const text =
  '{"name":"John", "birth":"1986-12-14", "city":"New York"}';
  const obj = JSON.parse(text);
obj.birth = new Date(obj.birth);
  
document.getElementById("demo").innerHTML = obj.name + ", " + obj.birth;

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>Convert a string into a date object.</h2>
<p id="demo"></p>

<script>
const text = '{"name":"John", "birth":"1986-12-14", "city":"New York"}';
const obj = JSON.parse(text);
obj.birth = new Date(obj.birth);
document.getElementById("demo").innerHTML = obj.name + ", " + obj.birth; 
</script>

</body>
</html>

.ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ reviver ﻡﺎﻧ ﻪﺑ JSON.parse()

.ﺪﻨﮐ ﯽﻣ ﯽﺳﺭﺮﺑ ﺍﺭ ﯽﮔﮋﯾﻭ ﺮﻫ ﻪﮐ ﺖﺳﺍ ﯽﻌﺑﺎﺗ reviver ﺮﺘﻣﺍﺭﺎﭘ

ﻝﺎﺜﻣ

:ﺪﯿﻨﮐ ﻞﯾﺪﺒﺗ ﺦﯾﺭﺎﺗ ﻪﺑ ﺍﺭ ﻪﺘﺷﺭ ﮏﯾ reviver ﻊﺑﺎﺗ ﺯﺍ ﻩﺩﺎﻔﺘﺳﺍ ﺎﺑ

const text =
  '{"name":"John", "birth":"1986-12-14", "city":"New York"}';
  const obj = JSON.parse(text, function (key, value) {
  if 
  (key == "birth") {
    return new 
  Date(value);
  } else {
    
  return value;
  }
});
  
document.getElementById("demo").innerHTML = obj.name + ", " + obj.birth;

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>Convert a string into a date object.</h2>

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

<script>
const text = '{"name":"John", "birth":"1986-12-14", "city":"New York"}';
const obj = JSON.parse(text, function (key, value) {
  if (key == "birth") {
    return new Date(value);
  } else {
    return value;
  }
});
document.getElementById("demo").innerHTML = obj.name + ", " + obj.birth; 
</script>

</body>
</html>

ﻪﯾﺰﺠﺗ ﻊﺑﺍﻮﺗ

.ﺪﻨﺘﺴﯿﻧ ﺯﺎﺠﻣ JSON ﺭﺩ ﻊﺑﺍﻮﺗ

.ﺪﯿﺴﯾﻮﻨﺑ ﯼﺍ ﻪﺘﺷﺭ ﺕﺭﻮﺻ ﻪﺑ ﺍﺭ ﻥﺁ ،ﺪﯾﺭﺍﺩ ﻊﺑﺎﺗ ﮏﯾ ﻥﺩﺮﮐ ﻪﻓﺎﺿﺍ ﻪﺑ ﺯﺎﯿﻧ ﺮﮔﺍ

:ﺪﯿﻨﮐ ﻞﯾﺪﺒﺗ ﻊﺑﺎﺗ ﮏﯾ ﻪﺑ ﺍﺭ ﻥﺁ ﺍًﺪﻌﺑ ﺪﯿﻧﺍﻮﺗ ﯽﻣ

ﻝﺎﺜﻣ

:ﻊﺑﺎﺗ ﻪﺑ ﻪﺘﺷﺭ ﻞﯾﺪﺒﺗ

const text =
  '{"name":"John", "age":"function () {return 
  30;}", "city":"New York"}';
  const obj = JSON.parse(text);
obj.age = eval("(" + obj.age + ")");
  
document.getElementById("demo").innerHTML = obj.name + ", " + 
  obj.age();

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>Convert a string into a function.</h2>
<p id="demo"></p>

<script>
const text = '{"name":"John", "age":"function() {return 30;}", "city":"New York"}';
const obj = JSON.parse(text);
obj.age = eval("(" + obj.age + ")");
document.getElementById("demo").innerHTML = obj.name + ", " + obj.age(); 
</script>

</body>
</html>

،ﺩﺍﺩ ﺪﻨﻫﺍﻮﺧ ﺖﺳﺩ ﺯﺍ ﺍﺭ ﺩﻮﺧ ﻪﻨﻣﺍﺩ ﻊﺑﺍﻮﺗ ،ﺪﯿﻨﮐ ﺏﺎﻨﺘﺟﺍ JSON ﺭﺩ ﻊﺑﺍﻮﺗ ﺯﺍ ﻩﺩﺎﻔﺘﺳﺍ ﺯﺍ ﺪﯾﺎﺑ

تمامی حقوق محفوظ است. © BasicIT.org • 2023-2024