JSON.stringify()的深度使用

來源:互聯網
上載者:User

標籤:efi   turn   使用   class   log   pac   return   bsp   負數   

在使用JSON.stringify()對JSON資料進行序列化時

1> 如果裡面的屬性是function,則會被忽略

const data = {     a: ‘a‘,     fn: funciton() {           return true         }   }JSON.stringify(data); // "{"a":"a"}"    *******fn屬性被忽略了**********

2> 如果裡面的屬性的值是undefined, 也是會被忽略的

const data = {     a: ‘a‘,     b: undefined}JSON.stringify(data); // "{"a":"a"}"    *******b屬性被忽略了**********

3>但是如果裡面的屬性值是null, 是不會被忽略的

const data = {     a: ‘a‘,     b: null}JSON.stringify(data); // "{"a":"a","b":null}"    *******b屬性沒有被忽略**********

其實JSON.stringify()有三個參數:stringify(value, [replacer, space](可選的, replacer: 自訂的函數,space: 格式化輸出(相當於tab鍵,值的範圍是[1(負數的時候預設是1),10]))),為了屬性值為function和undefined的屬性在序列化的時候不要被忽略,我們可以對replacer做操作

比如:

 

JSON.stringify()的深度使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.