帶有function的JSON對象的序列化與還原

JSON對象的序列化與反序列化相信大家都很熟悉了?;镜腶pi是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)
var json = JSON.parse(s)

ok 到現(xiàn)在為止都沒啥問題,處理得很好,但是現(xiàn)在我有這么一個json對象

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

我們看下JSON.stringify(json)輸出啥

"{"name":"json"}"

把getName弄丟了 ,怎么辦呢?其實大家都沒注意到JSON.stringify還有些參數(shù)

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.
那我們現(xiàn)在就可以把函數(shù)也序列化了

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

ok現(xiàn)在我們已經(jīng)成功的序列化帶function的json對象了,接下來如何還原它呢?

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;
});
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容