javascript 中的JSON.stringify - 將對象和數組轉換爲json格式(來源於網絡)

 
 
 

JSON.stringify 函數 (JavaScript)

 

將 JavaScript 值轉換爲 JavaScript 對象表示法 (Json) 字符串。javascript

 
 
JSON.stringify(value [, replacer] [, space])

參數
 
value

必需。 要轉換的 JavaScript 值(一般爲對象或數組)。java

replacer

可選。 用於轉換結果的函數或數組。json

若是 replacer 爲函數,則 JSON.stringify 將調用該函數,並傳入每一個成員的鍵和值。 使用返回值而不是原始值。 若是此函數返回 undefined,則排除成員。 根對象的鍵是一個空字符串:""。數組

若是 replacer 是一個數組,則僅轉換該數組中具備鍵值的成員。 成員的轉換順序與鍵在數組中的順序同樣。當 value 參數也爲數組時,將忽略 replacer 數組。函數

space

可選。 向返回值 JSON 文本添加縮進、空格和換行符以使其更易於讀取。ui

若是省略 space,則將生成返回值文本,而沒有任何額外空格。this

若是 space 是一個數字,則返回值文本在每一個級別縮進指定數目的空格。 若是 space 大於 10,則文本縮進 10 個空格。spa

若是 space 是一個非空字符串(例如「\t」),則返回值文本在每一個級別中縮進字符串中的字符。.net

若是 space 是長度大於 10 個字符的字符串,則使用前 10 個字符。code

一個包含 JSON 文本的字符串。 



備註
 

若是 value 具備 toJSON 方法,則 JSON.stringify 函數將使用該方法的返回值。 若是 toJSON 方法的返回值爲undefined,則不轉換成員。 這使對象可以肯定本身的 JSON 表示形式。

將不會轉換不具備 JSON 表示形式的值,例如 undefined。 在對象中,將丟棄這些值。 在數組中,會將這些值替換爲 null。

字符串值以引號開始和結束。 全部 Unicode 字符可括在引號中,但必須使用反斜槓進行轉義的字符除外。 如下字符的前面必須是反斜槓:

  • 引號 (")

  • 反斜槓 (\)

  • 退格鍵 (b)

  • 換頁符 (f)

  • 換行符 (n)

  • 回車符 (r)

  • 水平製表符 (t)

  • 四個十六進制數字 (uhhhh)

 

 

此示例使用 JSON.stringify 將 contact 對象轉換爲 JSON 文本。 定義 memberfilter 數組以便只轉換 surname 和phone 成員。 省略 firstname 成員。

 
var contact = new Object();
contact.firstname = "Jesper";
contact.surname = "Aaberg";
contact.phone = ["555-0100", "555-0120"];

var memberfilter = new Array();
memberfilter[0] = "surname";
memberfilter[1] = "phone";
var jsonText = JSON.stringify(contact, memberfilter, "\t");
document.write(jsonText);
// Output:
// { "surname": "Aaberg", "phone": [ "555-0100", "555-0120" ] }

 

 

此示例將 JSON.stringify 與一個數組一塊兒使用。 replaceToUpper 函數將數組中的每一個字符串轉換爲大寫形式。

 
var continents = new Array();
continents[0] = "Europe";
continents[1] = "Asia";
continents[2] = "Australia";
continents[3] = "Antarctica";
continents[4] = "North America";
continents[5] = "South America";
continents[6] = "Africa";

var jsonText = JSON.stringify(continents, replaceToUpper);

function replaceToUpper(key, value) {
    return value.toString().toUpperCase();
}

//Output:
// "EUROPE,ASIA,AUSTRALIA,ANTARCTICA,NORTH AMERICA,SOUTH AMERICA,AFRICA"

 

 

此示例使用 toJSON 方法將字符串值轉換爲大寫形式。

 
var contact = new Object();
contact.firstname = "Jesper";
contact.surname = "Aaberg";
contact.phone = ["555-0100", "555-0120"];

contact.toJSON = function(key)
 {
    var replacement = new Object();
    for (var val in this)
    {
        if (typeof (this[val]) === 'string')
            replacement[val] = this[val].toUpperCase();
        else
            replacement[val] = this[val]
    }
    return replacement;
};

var jsonText = JSON.stringify(contact);
document.write(jsonText);

// Output:
{"firstname":"JESPER","surname":"AABERG","phone":["555-0100","555-0120"]}



'{"firstname":"JESPER","surname":"AABERG","phone":["555-0100","555-0120"]}'
*/

 

要求
 

在如下文檔模式中受到支持:Internet Explorer 8 標準模式、Internet Explorer 9 標準模式、Internet Explorer 10 標準模式、Internet Explorer 11 標準模式。此外,也在應用商店應用(Windows 8 和 Windows Phone 8.1)中受支持。請參閱版本信息

在如下文檔模式中不受支持:Quirks、Internet Explorer 6 標準模式、Internet Explorer 7 標準模式。

相關文章
相關標籤/搜索