json

JSON with javascript

Json ဆိုတာကတောာ့ avaScript Object Notation ဖြစ်ပြီး အခုနောက်ပိုင်း အသုံးများတဲ့ format ဆိုလည်း မမှားပါဘူး ။ web developer ဆိုရင်လည်း json ကို သိထားသင့်ပြီး iOS developer တွေ အနေနဲ့လည်း apple က plist နဲ့ တူတဲ့ အတွက်ကြောင့် သိထားသင့်ပါတယ်။json ကို JavaScript မှာ လွယ်လွယ်ကူကူ အသုံးပြုနိုင်ပါတယ်။ အခြား ဘာ addon မှ မလိုပဲ အသုံးပြုနိုင်သလို php မှာလည်း ပါပြီးသားပါ။ language တော်တော်များများမှာတော့ JSON ကို ထည့်သွင်းလာပါတယ်။ နောက်ပြီး web api တွေ အများစုကလည်း json ကို အသုံးပြုပါတယ်။ facebook ရဲ့ graph api ဆိုလည်း JSON ပါပဲ။

nosql တွေကို အသုံးပြုချင်တဲ့သူတွေအနေနဲ့ကလည်း JSON ကို လေ့လာထားဖို့လိုပါတယ်။ JSON format ကို mongodb မှာ သုံးထားတာကို တွေ့နိုင်ပါတယ်။ MongoDB ဟာ nosql database တွေထဲမှာ လူသုံးအများဆုံး database ဆိုလည်း မမှားပါဘူး။ JSON sample လေးကို ကြည့်ရအောင်။

{
"goku":{
			"gentel":"male",
			"height":180,
			"attack":8,
			"defend":3,
			"mana":{
						"speed":5,
						"health":3
			},
			"speed":[5,8,3]

		},
"luffy":{
			"gentel":"male",
			"height":178,
			"attack":5,
			"defend":4,
			"mana":{
						"speed":7,
						"health":3
			},
			"speed":[7,9,1]

		}

}

xml ကို သိတဲ့ လူတွေ အနေနဲ့ JSON ကို ကြည့်လိုက်တာနဲ့ သဘောပေါက်နိုင်ပါတယ်။  goku ရဲ့ properties နဲ့ luffy ရဲ့ properties ကို JSON နဲ့ ရေးထားလိုက်တာပါ။ Object ဆိုရင်တော့ {} နဲ့ ရေးပြီး string ဆိုရင်တော့ “string” ဆိုပြီး ရေးပါတယ်။ value နဲ့ name ကို

{“name”:”value”}

ဆိုပြီး ရေးပါတယ်။ value က number ဆိုရင်တော့ “” မလိုပါဘူး။ Array ဆိုရင်တော့ [ ] နဲ့ရေးပါတယ်။

goku ရဲ့ attack ဘယ်လောက်ရှိသလဲ သိချင်ရင် javascript နဲ့ ဒီလိုရေးလို့ရပါတယ်။

<html>
<head>
<title>JSON Test</title>
<script>
var json={
"goku":{
			"gentel":"male",
			"height":180,
			"attack":8,
			"defend":3,
			"mana":{
						"speed":5,
						"health":3
			},
			"speed":[5,8,3]

		},
"luffy":{
			"gentel":"male",
			"height":178,
			"attack":5,
			"defend":4,
			"mana":{
						"speed":7,
						"health":3
			},
			"speed":[7,9,1]

		}

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

<script>
goku=json['goku'];

alert(goku['attack']);
</script>
</body>
</html>

sql သုံးသလို query တွေ ရေးစရာမလိုပဲ တန်းပြီး index ထောက်ပြီး ခေါ်လိုက်ရုံပါပဲ။ JSON က search အတွက် SQL လိုမျိုးတော့ မကောင်းပါဘူး။ index ထောက်ပြီး data ခေါ်မယ်ဆိုရင်တော့ တော်တော်လေးကို အသုံးဝင်ပါတယ်။ ထပ်ပြီး လေ့လာချင်ရင်တော့ source code file လေးကို download ချပြီး လေ့လာနိုင်ပါတယ်။ မသိတာရှိရင်လည်း comment မှာ မေးခဲ့နိုင်ပါတယ်။

This entry was posted in Javascript, Web Essential and tagged on by .

About saturngod

iOS Game Developer အနေနဲ့ လက်ရှိ အလုပ်လုပ်နေတယ်။ http://www.saturngod.net မှာ blog ရေးတယ်။ နောက်ပြီး http://en.saturngod.net မှာလည်း ရေးဖြစ်တယ်။ twitter မှာဆိုရင်တော့ http://www.twitter.com/saturngod မှာ ရှိပါတယ်။

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>