जावास्क्रिप्ट JSON स्ट्रिंग () विधि
उदाहरण
जावास्क्रिप्ट ऑब्जेक्ट को स्ट्रिंग करें:
var obj = { "name":"John", "age":30, "city":"New York"};
var myJSON =
JSON.stringify(obj);
document.getElementById("demo").innerHTML = myJSON;
नीचे और अधिक "इसे स्वयं आज़माएं" उदाहरण।
परिभाषा और उपयोग
JSON.stringify () विधि जावास्क्रिप्ट ऑब्जेक्ट्स को स्ट्रिंग्स में कनवर्ट करती है।
वेब सर्वर पर डेटा भेजते समय डेटा को एक स्ट्रिंग होना चाहिए।
ब्राउज़र समर्थन
तालिका में संख्याएं पहले ब्राउज़र संस्करण को निर्दिष्ट करती हैं जो पूरी तरह से विधि का समर्थन करता है।
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. |
टेक्निकल डिटेल
प्रतिलाभ की मात्रा: | एक स्ट्रिंग |
---|---|
जावास्क्रिप्ट संस्करण: | ईसीएमएस्क्रिप्ट 5 |
और ज्यादा उदाहरण
उदाहरण
प्रतिकृति फ़ंक्शन का उपयोग करना :
/*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;
}
});
उदाहरण
अंतरिक्ष पैरामीटर का उपयोग करना :
/*Insert
10 space characters for each white space:*/
var obj = { "name":"John",
"age":"39", "city":"New York"};
var text = JSON.stringify(obj,
null, 10);
उदाहरण
अंतरिक्ष पैरामीटर का उपयोग करना :
/*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 परिचय