JavaScript Lesson: JSON

JSON adalah singkatan bagi JavaScript Object Notation. JSON menggunakan JavaScript syntax tetapi dalam bentuk text sahaja. JSON adalah format bagi menyimpan dan menghantar data.

Contoh data JSON.

{"employees":[
    {"firstName":"John", "lastName":"Doe"},
    {"firstName":"Anna", "lastName":"Smith"},
    {"firstName":"Peter", "lastName":"Jones"}
]}

Menukarkan JSON text kepada JavaScript object.

<p id="demo"></p>
<script>
var text = '{"name":"Shahrul Nizam","website":"shahrulnizam.com","phone":"013-6289844"}';
var obj = JSON.parse(text);

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

JSON digunakan kerana lebih laju dan senang berbanding XML di dalam aplikasi AJAX. File JSON adalah jenis “.json”

Contoh data JSON untuk http://shahrulnizam.com/iot/thing-speak/
di https://api.thingspeak.com/channels/63524/feeds.json

Penerangan lanjut di

  1. http://www.w3schools.com/json/json_syntax.asp
  2. http://www.w3schools.com/json/json_eval.asp
  3. http://www.w3schools.com/json/json_http.asp

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.