JSon: Pengenalan

March 12, 2016

EH. Di post sebelumnya, saya hanya curcol tentang kzl nya harus mengkoding mengikuti teknologi. Jangan gitu dong, jelek-jelek, hidup saya dari perkembangan teknologi itu. Seharusnya saya memberi contoh dan motivasi hihihi.. HATE turns into Love, semoga. Then jadi love and hate collide. Baiklah, kita mulai lagi dari awal.Iya saya mah begitu, suka memulai dari awal lagi.

Belajar JSon bisa dari situsnya: json.org

JSon itu JavaScript Object Nation. Format untuk data-interchange. Dari sisi manusia, mudah dibaca dan ditulis. Dari sisi mesin, mudah untuk diparse dan dibangkitkan. JSon berbentuk format teks, language independent tetap dengan konvensi yang biasa digunakan programmer dari keluarga C. Sudah tahu siapa saja keluarga C? Itulah. C, C++, C#, Java, JavaScript, Perl, Python. Dan juga PHP. (dasar kamu, PHP!)

Struktur JSon ada dua macam:

  • A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
  • An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

Contoh JSon

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

 

Bandingkan dengan XML

<employees>
<employee>
<firstName>John</firstName> <lastName>Doe</lastName>
</employee>
<employee>
<firstName>Anna</firstName> <lastName>Smith</lastName>
</employee>
<employee>
<firstName>Peter</firstName> <lastName>Jones</lastName>
</employee>
</employees>

 

Sebetulnya JSon itu secara format is syntactically identical to the code for creating JavaScript objects.

 

Sebagai contoh:

<!DOCTYPE html>
<html>
<body><h2>JSON Object Creation in JavaScript</h2>

<p id=“demo”></p>

<script>
var text = ‘{“name”:”John Johnson”,”street”:”Oslo West 16″,”phone”:”555 1234567″}’;

var obj = JSON.parse(text);

document.getElementById(“demo”).innerHTML =
obj.name + “<br>” +
obj.street + “<br>” +
obj.phone;
</script>

</body>
</html>

akan menghasilkan:

JSON Object Creation in JavaScript

John Johnson
Oslo West 16
555 1234567

ok ok

Sekarang saya tahu kenapa saya ga suka JSon. Karena saya ga suka JavaScript meski dia sama2 Java ya 😪

JSon, duh harusnya aku tulis JSON ya?

Seperti XML, karena:

  • Both JSON and XML is “self describing” (human readable)
  • Both JSON and XML is hierarchical (values within values)
  • Both JSON and XML can be parsed and used by lots of programming languages
  • Both JSON and XML can be fetched with an XMLHttpRequest

Sangat berbeda dengan XML, karena:

  • JSON doesn’t use end tag
  • JSON is shorter
  • JSON is quicker to read and write
  • JSON can use arrays

Perbedaan yang sangat mendasar:

XML has to be parsed with an XML parser, JSON can be parsed by a standard JavaScript function.

Ngerti? Enggak 😁😁😁

WHY JSON. Begini katanya:

For AJAX applications, JSON is faster and easier than XML:

Using XML

  • Fetch an XML document
  • Use the XML DOM to loop through the document
  • Extract values and store in variables

Using JSON

  • Fetch a JSON string
  • JSON.Parse the JSON string

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: