码迷,mamicode.com
首页 > Web开发 > 详细

JSON.stringify()的深度使用

时间:2017-07-27 20:23:40      阅读:157      评论:0      收藏:0      [点我收藏+]

标签: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()的深度使用

标签:efi   turn   使用   class   log   pac   return   bsp   负数   

原文地址:http://www.cnblogs.com/yyh1/p/7246490.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!