JavaScript JSON stringify() နည်းလမ်း
ဥပမာ
JavaScript object တစ်ခုကို ခိုင်မာအောင်ပြုလုပ်ပါ-
var obj = { "name":"John", "age":30, "city":"New York"};
var myJSON =
JSON.stringify(obj);
document.getElementById("demo").innerHTML = myJSON;
အောက်တွင် နောက်ထပ် "စမ်းသုံးကြည့်ပါ" ဥပမာများ။
အဓိပ္ပါယ်နှင့် အသုံးပြုမှု
JSON.stringify() နည်းလမ်းသည် JavaScript အရာဝတ္ထုများကို စာကြောင်းများအဖြစ် ပြောင်းလဲပေးသည်။
ဝဘ်ဆာဗာတစ်ခုထံ ဒေတာပေးပို့သည့်အခါ ဒေတာသည် စာကြောင်းတစ်ခု ဖြစ်ရပါမည်။
Browser ပံ့ပိုးမှု
ဇယားရှိ နံပါတ်များသည် နည်းလမ်းကို အပြည့်အဝထောက်ခံသည့် ပထမဆုံးဘရောက်ဆာဗားရှင်းကို သတ်မှတ်ပေးသည်။
Method | |||||
---|---|---|---|---|---|
stringify() | 4.0 | 8.0 | 3.5 | 4.0 | 11.5 |
အထားအသို
JSON.stringify(obj, replacer, space)
ကန့်သတ်တန်ဖိုးများ
Parameter | Description |
---|---|
obj | Required. The value to convert to a string |
replacer | Optional. Either a function or an array used to transform the result. The replacer is called for each item. |
space | Optional. Either a String or a Number. A string to be used as white space (max 10 characters), or a Number, from 0 to 10, to indicate how many space characters to use as white space. |
နည်းပညာဆိုင်ရာအသေးစိတ်
ပြန်ပို့တန်ဖိုး- | ကြိုးတစ်ချောင်း |
---|---|
JavaScript ဗားရှင်း- | ECMAScript ၅ |
နောက်ထပ် ဥပမာများ
ဥပမာ
အစားထိုး လုပ်ဆောင်ချက်ကို အသုံးပြုခြင်း -
/*replace the value of "city" to upper case:*/
var obj = { "name":"John",
"age":"39", "city":"New York"};
var text = JSON.stringify(obj, function
(key, value) {
if (key == "city") {
return value.toUpperCase();
} else {
return value;
}
});
ဥပမာ
space parameter ကိုအသုံးပြုခြင်း -
/*Insert
10 space characters for each white space:*/
var obj = { "name":"John",
"age":"39", "city":"New York"};
var text = JSON.stringify(obj,
null, 10);
ဥပမာ
space parameter ကိုအသုံးပြုခြင်း -
/*Insert
the word SPACE for each white space:*/
var obj = { "name":"John",
"age":"39", "city":"New York"};
var text = JSON.stringify(obj,
null, "SPACE");
ဆက်စပ်စာမျက်နှာများ
JSON ကျူတိုရီရယ်- JSON မိတ်ဆက်