JSON对象的序列化与反序列化相信大家都很熟悉了。基本的api是JSON.parse与JSON.stringify.

复制代码
var json={
    uiModule:'http://www.a.com',
    login:'true',
    mainSubjectId:3004,
    happydays:100,
    happyhours:1,
    userCount :200,
    itemCount:1000000,
    type:'all',
    mainSubjectId:3004,
    taglist:[
        {'tagName':'xiaoc','applyItemCount':20},
        {'tagName':'xiaoc','applyItemCount':20}
    ]
}

var s=JSON.stringify(json)
复制代码

 

输出:

复制代码
"{"uiModule":"http://www.a.com","login":"true","mainSubjectId":3004,"happydays":100,"happyhours":1,"userCount":200,"itemCount":1000000,"type":"all","taglist":[{"tagName":"xiaoc","applyItemCount":20},{"tagName":"xiaoc","applyItemCount":20}]}"

JSON.parse(s)
复制代码

 

输出:

 ok 到现在为止都没啥问题,处理得很好,但是现在我有这么一个json对象

复制代码
var json={
  name:'json',
  getName:function(){
     return this.name;   
  }
}
复制代码

 

我们看下JSON.stringify(json)输出啥

"{"name":"json"}"

尼玛把getName弄丢了 ,怎么办呢?其实大家都没注意到JSON.stringify还有些参数

 

复制代码
JSON.stringify(value [, replacer] [, space])

value

Required. A JavaScript value, usually an object or array, to be converted.

replacer

Optional. A function or array that transforms the results.

If replacer is a function, JSON.stringify calls the function, passing in the key and value of each member. The return value is used instead of the original value. If the function returns undefined, the member is excluded. The key for the root object is an empty string: "".

If replacer is an array, only members with key values in the array will be converted. The order in which the members are converted is the same as the order of the keys in the array. The replacer array is ignored when thevalue argument is also an array.

space

Optional. Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.

If space is omitted, the return-value text is generated without any extra white space.

If space is a number, the return-value text is indented with the specified number of white spaces at each level. Ifspace is greater than 10, text is indented 10 spaces.

If space is a non-empty string, such as '\t', the return-value text is indented with the characters in the string at each level.

If space is a string that is longer than 10 characters, the first 10 characters are used.
复制代码

 

那我们现在就可以把函数也序列化了

复制代码
var s=JSON.stringify(json, function(key, val) {
  if (typeof val === 'function') {
    return val + '';
  }
  return val;
});


"{"name":"json","getName":"function (){\n     return this.name;   \n  }"}"
复制代码

 

ok现在我们已经成功的序列化带function的json对象了,接下来如何还原它呢?

直接JSON.parse(s)?  骚年你还是太年轻了。

JSON.parse(s)输出的是

其实JSON.parse和JSON.stringify一样也有些其他参数

复制代码
JSON.parse(text [, reviver])

text
  Required. A valid JSON string.
reviver
  Optional. A function that transforms the results. This function is called for each member of the object. If a member contains nested objects, the nested objects are transformed before the parent object is. For each member, the following occurs:
If reviver returns a valid value, the member value is replaced with the transformed value.
If reviver returns the same value it received, the member value is not modified.
If reviver returns null or undefined, the member is deleted.
复制代码

 

那么我们就可以这么来还原json对象

复制代码
JSON.parse(s,function(k,v){

  if(v.indexOf&&v.indexOf('function')>-1){

     return eval("(function(){return "+v+" })()")

  }

  return v;

});
复制代码

输出:

通过这种方式我们也可以完全深拷贝一个json对象了。

参考资料:http://msdn.microsoft.com/en-us/library/ie/cc836466(v=vs.94).aspx

posted @ 2018-01-05 11:22 星火spark 阅读(3954) 评论(1) 推荐(1) 编辑
摘要: 今天开发项目是,需要用到表格,于是想到,之前的项目中使用过datatables插件,于是就拿了过来,但是发现无法识别 Uncaught TypeError: $(...).DataTable is not a function 于是网上搜索 结果说是jquery冲突 jQuery.noConflic 阅读全文
posted @ 2017-11-24 15:37 星火spark 阅读(893) 评论(0) 推荐(0) 编辑
摘要: 如果在内网段中部署或者启动缓存服务。不能存在相同的组名称。如同使用dubbo一样,会导致无法连接到缓存节点 阅读全文
posted @ 2017-10-24 15:12 星火spark 阅读(289) 评论(0) 推荐(0) 编辑
摘要: bootstrap datepicker 初始化时,会给控件添加hasDatepicker类 ,如果此时调用 $singleDay.datepicker(initDayOpts);无法弹出时间控件 需要先移除hasDatepicker $singleDay.removeClass("hasDatep 阅读全文
posted @ 2017-10-24 15:11 星火spark 阅读(1255) 评论(0) 推荐(0) 编辑
摘要: 之前项目中的web.xml中的编码设置: 但这个设置是针对POST请求的,tomacat对GET和POST请求处理方式是不同的,要处理针对GET请求的编码问题,则需要改tomcat的server.xml配置文件,如下: 改为: 最关键的点在这里:如果你是更改的tomcat安装目录的server.xm 阅读全文
posted @ 2017-10-23 18:42 星火spark 阅读(344) 评论(0) 推荐(0) 编辑
摘要: 使用Spring boot整合Hive,在启动Spring boot项目时,报出异常: 1 经过排查,是maven的包冲突引起的,具体做法,排除:jetty-all、hive-shims依赖包。对应的pom配置如下: 阅读全文
posted @ 2017-10-11 14:50 星火spark 阅读(10688) 评论(0) 推荐(0) 编辑
摘要: 在写SQL 条件语句是经常用到 不等于‘<>’的筛选条件,此时要注意此条件会将字段为null的数据也当做满足不等于的条件而将数据筛选掉。 例:表A 用 select * from A where B1<>1查询时得到的结果为: 第三列 B1为空的也是会筛选掉的。 要查出第三列只需将SQL 改为 :s 阅读全文
posted @ 2017-07-27 13:49 星火spark 阅读(1104) 评论(0) 推荐(0) 编辑
摘要: 下面是buffers与cached的区别。 buffers是指用来给块设备做的缓冲大小,他只记录文件系统的metadata以及 tracking in-flight pages. cached是用来给文件做缓冲。 那就是说:buffers是用来存储,目录里面有什么内容,权限等等。 而cached直接 阅读全文
posted @ 2017-07-13 17:34 星火spark 阅读(308) 评论(0) 推荐(0) 编辑
摘要: Linux下频繁读写文件时,内存资源被耗尽,当程序结束后,内存不会释放需要清除缓存。Linux缓存有dentry,buffer cache,page cache。 注:Dentry用来加速文件路径名到inode的转换;buffer cache加速磁盘块的读写;page cache加速inode的读写 阅读全文
posted @ 2017-07-13 17:33 星火spark 阅读(940) 评论(0) 推荐(0) 编辑
摘要: 查看CPU信息(型号) cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c 查看内 存信息# cat /proc/meminfo 阅读全文
posted @ 2017-07-13 17:30 星火spark 阅读(1631) 评论(0) 推荐(0) 编辑
点击右上角即可分享
微信分享提示