带有function的JSON对象的序列化与还原
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
带有function的JSON对象的序列化与还原的更多相关文章
- 利用JavaScriptSerializer类 进行Json对象的序列化和反序列化和过滤
项目下载:JavaScriptSerializer_对JSON对象序列化与反序列化及过滤器 利用<JavascriptSerializer类> 进行Json对象的序列化和反序列化 1. 首 ...
- JavaScript - 问题集 - 含function的json对象与json字符串之间相互转换
基本的转换为:JSON.parse与JSON.stringify. 但是json数据中含function,则转换后,function会丢失,如: var json={ test:'test', log ...
- return Json对象时序列化错误
当要序列化的表与另一个表是一对多的关系是,表1序列化时会找到另一个表2关联的字段,会将另一个表2进行序列化,然后表2中也有一个字段与表1关联,这样序列化就会产生循环序列化. 在网上进行搜索,其中大多数 ...
- function的json对象转换字符串与字符串转换为对象的方法
// json对象转换成字符串var str = JSON.stringify(json, function(key, val) { if (typeof val === 'function') { ...
- 利用<JavascriptSerializer类> 进行Json对象的序列化和反序列化
1. 首先, JavascriptSerializer类所在名空间: using System.Web.Script.Serialization; 2. 相关的3篇文章, 标记下: 使用JavaScr ...
- javascript对json对象的序列化与反序列化
首先引入一个json2.js.官方的地址为:https://github.com/douglascrockford/JSON-js 这里为了方便我直接贴上源代码 /* json2.js 2013-05 ...
- js实现对json数据的序列化(兼容ie6以上浏览器)
/** * 增加对JSON数据的序列化方法, * 主要用于IE6.7不支持JSON对象的浏览器 */ var xue = xue || {};xue.json = xue.json || {}; xu ...
- ng json格式的序列化和反序列化
ng中自带方法 angular.toJson 序列化angular.fromJson 反序列化 结果: 代码: <!DOCTYPE html> <html ng-app=" ...
- javascript中字符串格式json如何转化成json对象
什么是JSON JSON(JavaScript Object Notation)是一种优美的JavaScript对象创建方法.JSON也是一种轻量级数据交换格式.JSON非常易于人阅读与编写,同时利于 ...
随机推荐
- Netty 源码剖析之 unSafe.write 方法
前言 在 Netty 源码剖析之 unSafe.read 方法 一文中,我们研究了 read 方法的实现,这是读取内容到容器,再看看 Netty 是如何将内容从容器输出 Channel 的吧. 1. ...
- Windows7下IIS+php配置教程 http://www.jb51.net/article/113812.htm
这篇文章主要为大家详细介绍了Windows7下IIS+php配置教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 WINDOWS 7 IIS+php配置教程,具体内容如下 打开 开始 -> ...
- $.each()和$(selector).each()
转载:http://www.jb51.net/article/65215.htm $.each()与$(selector).each()不同, 后者专用于jquery对象的遍历, 前者可用于遍历任何的 ...
- 彻底理解ReentrantLock
5.ReentrantLock的介绍 ReenTrantLock重入锁,是实现Lock接口的一个类,也是在实际编程中使用频率很高的一个锁,支持重入性,表示能够对共享资源能够重复加锁,即当前线程获取该锁 ...
- 面试问题:你了解Java内存模型么(Java7、8、9内存模型的区别)
Java内存模型是每个java程序员必须掌握理解的,这是Java的核心基础,对我们编写代码特别是并发编程时有很大帮助.由于Java程序是交由JVM执行的,所以我们在谈Java内存区域划分的时候事实上是 ...
- JS获取元素属性
<style> *{ box-sizing: border-box; } html, body { margin: 0px; width: 100%; height: 100%; over ...
- SpringMVC拦截器实现:当用户访问网站资源时,监听session是否过期
SpringMVC拦截器实现:当用户访问网站资源时,监听session是否过期 一.拦截器配置 <mvc:interceptors> <mvc:interceptor> < ...
- JConsole连接远程linux服务器配置
1.在远程机的tomcat的catalina.sh中加入配置 (catalina.sh路径在tomcat/bin下面 如/usr/local/tomcat/bin) if [ "$1&quo ...
- node 静态伺服(搭建服务)
基本功能 不急着写下第一行代码,而是先梳理一下就基本功能而言有哪些步骤. 在本地根据指定端口启动一个http server,等待着来自客户端的请求 当请求抵达时,根据请求的url,以设置的静态文件目录 ...
- 转 Fira Code | 为写程序而生的字体
原文:Fira Code | 为写程序而生的字体 Fira Code | 为写程序而生的字体 己短不可藏 6月前 · 1199 人阅读 关注TA 程序员福利!!!今天为大家带来一个专为程序员写程序设计 ...