Parsing JSon dengan Javascript


tags:json,javascript,json parsing,tutorial json,parsing json,belajar Json,pengenalan JSon.

contoh:

1. {“saya”:”yusuf”}

2. {“hobi”:[{“nama_hobi”:”debus”},{“nama_hobi”:”Kuda Kudaan”}, {“nama_hobi”:”pacaran”}]}

Bukan {“hobi”:[{“nama_hobi”:”debus”, “nama_hobi”:”Kuda Kudaan”, “nama_hobi”:”pacaran”}]}

Bukan {“hobi”:{“nama_hobi”:”debus”},{“nama_hobi”:”Kuda Kudaan”}, {“nama_hobi”:”pacaran”}}

    [ ] kurung siku menandakan array (hobi lebih dari 1) , kalo bukan array jadi :

   {“hobi”:{“nama_hobi”:”debus”}} atau {“hobi”:”debus”}

 

 

Selanjutnya Coba lihat Kode Ini (jangan lupa Jquerynya / sesuaikan sendiri🙂 ) :

<html>
<head>
<script type=”text/javascript” src=”jquery-1.7.0.min.js”></script>
<script  type=”text/javascript”>
$(document).ready(function() {
//1
var obj = jQuery.parseJSON(‘{“saya”:”yusuf”}’);
alert( “Javascript => saya sendiri:”+obj.saya );

//2
var obj2 = jQuery.parseJSON(‘{“kami”: [ {“nama”:”yusuf”}, {“nama”:”deden”} , {“nama”:”igun”} ] }’); //json array
var kami = obj2.kami;

alert(“Javascript => kami ber-“+kami.length);
alert(“Javascript => kami:”+kami[0].nama);
alert(“Javascript => kami:”+kami[1].nama);
alert(“Javascript => kami:”+kami[2].nama);

//3
// tambah hobi => {“hobi”:[{“nama_hobi”:”makan”,”nama_hobi”:”minum”,”nama_hobi”:”merokok”}]}
var obj3 = jQuery.parseJSON(‘{“kami”: [ {“nama”:”yusuf”,”hobi”:[{“nama_hobi”:”makan”},{“nama_hobi”:”minum”},{“nama_hobi”:”merokok”}]}, {“nama”:”deden”, “hobi”:[{“nama_hobi”:”makan1″},{“nama_hobi”:”minum1″},{“nama_hobi”:”merokok1″}]} , {“nama”:”igun”, “hobi”:[{“nama_hobi”:”makan2″},{“nama_hobi”:”minum2″},{“nama_hobi”:”koding”}] } ] }’); //json array

//3.1
var kami2 = obj3.kami;
alert(“Javascript => saya:”+kami2[0].nama);
var hobi = kami2[0].hobi;
alert(“Javascript => hobi si-“+kami2[0].nama+” : 1).”+hobi[0].nama_hobi+”, 2).”+hobi[1].nama_hobi+”, 3).”+hobi[2].nama_hobi);

//3.2
alert(“Javascript => saya:”+kami2[1].nama);
var hobi = kami2[1].hobi;
alert(“Javascript => hobi si-“+kami2[1].nama+” : 1).”+hobi[0].nama_hobi+”, 2).”+hobi[1].nama_hobi+”, 3).”+hobi[2].nama_hobi);

//3.1
alert(“Javascript => saya:”+kami2[2].nama);
var hobi = kami2[2].hobi;
alert(“Javascript => hobi si-“+kami2[2].nama+” : 1).”+hobi[0].nama_hobi+”, 2).”+hobi[1].nama_hobi+”, 3).”+hobi[2].nama_hobi);

});
</script>
</head>
<body>

</body>
</html>

Untuk menggunakan JQUERY di lain kesempatan.

Demikian tutorial singkan Tentang Json Parsing dengan Javascript ini, Smoga bermanfaat🙂

One thought on “Parsing JSon dengan Javascript

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