带有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非常易于人阅读与编写,同时利于 ...
随机推荐
- Mac Hadoop的安装与配置
这里介绍Hadoop在mac下的安装与配置. 安装及配置Hadoop 首先安装Hadoop $ brew install Hadoop 配置ssh免密码登录 用dsa密钥认证来生成一对公钥和私钥: $ ...
- unity 中Canvas MatchHeight
设置了 UI Scale Mode = Scale With Screen Size, Reference Resolution = X 1334, Y 750, Screen Match Mode ...
- unity中 TextMeshPro 的常用标签
这个第二和第三个写反了. 例子10中的123标签需要用到另一个字体,详情看 TextMeshPro 的官方示例10.
- [android] 优酷环形菜单-旋转动画
获取房子,菜单图标ImageView对象,获取三个圆环RelativeLayout对象 给菜单图标(icon_menu)设置点击事件 定义一个成员变量isLevel3Show来存储第三级菜单是否显示 ...
- Linux常用基本命令(chmod)
chmod命令用来改变文件或者目录的权限,只有文件的属主和超级用户才能够执行这个命令 格式: chmod [option] [mode] [file] >常用参数选项 -R : 递归修改目录以及 ...
- django-全文检索
全文检索不同于特定字段的模糊查询,使用全文检索的效率更高,并且能够对于中文进行分词处理 需要的第三方库: haystack:django的一个包,可以方便地对model里面的内容进行索引.搜索,设计为 ...
- 2016计蒜之道复赛 百度地图的实时路况(Floyd 分治)
题意 题目链接 Sol 首先一个结论:floyd算法的正确性与最外层\(k\)的顺序无关(只要保证是排列即可) 我大概想到一种证明方式就是把最短路树上的链拿出来,不论怎样枚举都会合并其中的两段,所以正 ...
- 浅谈对NaN的理解
1.NaN : Not a Number 不是一个数字 2.NaN 与其他数值进行比较的结果总是不相等的,包括它自身在内 3.判断是否是NaN, 方法一 :is.NaN(变量): 方法二 :Nu ...
- Microsoft MVP MSDN Magazine 最新期分享
下载地址:http://1105insight.com/portal/wts/uemcmQeeDyaq%5Ev2gAe6c3b0Djd 可在线或下载查看
- linux下安装mysql(ubuntu0.16.04.1)
安装步骤: sudo netstat -tap | grep mysql 查看是否已安装 安装mysql:sudo apt-get install mysql-server mysql-client ...