sailei1 / blog

1 stars 0 forks source link

JSON stringify parse 向下兼容 #61

Closed sailei1 closed 5 years ago

sailei1 commented 5 years ago

parse :

var json = '{"result":true, "count":42}';
//obj = JSON.parse(json);
//简易替代
obj=eval('(function(){ return'+json+'})()');

console.log(obj.count);
// expected output: 42

console.log(obj.result);
// expected output: true

stringify 比较麻烦 根据 值类型进行判断 复杂类型根据对象、数组做遍历,然后用'{'、'}'、'['、']'字符进行拼接,中间涉及到null、undefined 兼容 具体参考 老道前辈的str 函数

具体兼容方案 https://github.com/douglascrockford/JSON-js/blob/master/json2.js