json对象转字符串如何实现_Javascript教程-查字典教程网
json对象转字符串如何实现
json对象转字符串如何实现
发布时间:2016-12-30 来源:查字典编辑
摘要:背景:大部分浏览器已经实现了json对象转字符串的原生api支持,那在较低版本浏览器浏览器——如大家最喜爱的IE6——里如何实现呢?首先运行...

背景:大部分浏览器已经实现了json对象转字符串的原生api支持,那在较低版本浏览器浏览器——如大家最喜爱的IE6——里如何实现呢?

首先运行以下方法,测试各种情况下,JSON.stringify的输出,这有助于下文代码的实现以及测试。用例不一定完整,欢迎补充

复制代码 代码如下:

function test_toStringify(){

var result = {

"JSON.stringify(undefined)": JSON.stringify(undefined),

"JSON.stringify(null)": JSON.stringify(null),

"JSON.stringify(123)": JSON.stringify(123),

"JSON.stringify(true)": JSON.stringify(true),

"JSON.stringify('')": JSON.stringify(''),

"JSON.stringify('abc')": JSON.stringify('abc'),

"JSON.stringify(null)": JSON.stringify(null),

"JSON.stringify([1,2,3])": JSON.stringify([1,2,3]),

"JSON.stringify([undefined, undefined])": JSON.stringify([undefined, undefined]),

"JSON.stringify({name:'chyingp', age:24, u:undefined})": JSON.stringify({name:'chyingp', age:24, u:undefined})

};

var str = '';

for(var key in result){

if(typeof result[key] === 'string'){

str += key + " : '" + result[key] + "'n";

}else{

str += key + " : " + result[key] + "n";

}

}

console.log(str);

}

test_toStringify();

输出结果如下:

复制代码 代码如下:

JSON.stringify(undefined) : undefined

JSON.stringify(null) : 'null'

JSON.stringify(123) : '123'

JSON.stringify(true) : 'true'

JSON.stringify('') : '""'

JSON.stringify('abc') : '"abc"'

JSON.stringify([1,2,3]) : '[1,2,3]'

JSON.stringify([undefined, undefined]) : '[null,null]'

JSON.stringify({name:'chyingp', age:24, u:undefined}) : '{"name":"chyingp","age":24}'

下面是json对象转字符串的代码实现:

复制代码 代码如下:

function is_number(obj){ return Object.prototype.toString.call(obj)==='[object Number]'; }

function is_boolean(obj){ return Object.prototype.toString.call(obj)==='[object Boolean]'; }

function is_string(obj){ return Object.prototype.toString.call(obj)==='[object String]'; }

function is_null(obj){ return Object.prototype.toString.call(obj)==='[object Null]'; }

function is_undefined(obj){ return Object.prototype.toString.call(obj)==='[object Undefined]'; }

function is_object(obj){ return Object.prototype.toString.call(obj)==='[object Object]'; }

function is_array(obj){ return Object.prototype.toString.call(obj)==='[object Array]'; }

function is_function(obj){ return Object.prototype.toString.call(obj)==='[object Function]'; }

function quote(str){ return '"' + str + '"'; }

var basic_map = {

'[object Undefined]': true,

'[object Number]': true,

'[object Null]': true,

'[object Boolean]': true

}

function basic_type(obj){ return basic_map[ Object.prototype.toString.call(obj) ]; }

JSON = window.JSON || {};

//其实就是JSON.stringify

JSON.toStr = function(obj){

if(is_string(obj) || is_null(obj) || is_number(obj) || is_boolean(obj)) return quote(obj);

if(is_undefined(obj)) return obj;

if(is_array(obj)){

var left = "[",

middle = [],

right = "]",

value;

var callee = arguments.callee;

for(var i=0,len=obj.length; i<len; i++){

var value = obj[i];

if( typeof value === 'undefined' ){

middle.push(null+'');

}else{

if( basic_type(value) ){

middle.push( value )

}else{

middle.push( callee(obj[i]) )

}

}

}

return left+ middle.join(",") +right;

}

if(is_object(obj)){

var left = "{",

middle = [],

right = "}",

value ;

var callee = arguments.callee;

for(var key in obj){

var value = obj[key];

if(typeof obj[key] === 'undefined') continue;

if( basic_type(value) ){

middle.push( quote(key) + ':'+ value );

}else{

middle.push( quote(key) + ':'+ callee(value) );

}

}

return left + middle.join(', ') + right;

}

};

!JSON.stringify && (JSON.stringify = JSON.toStr);

以上代码仅为练手用,如有冗余及效率问题敬请见谅。如有错误则请帮忙指出 :)

PS:关于json操作,这里再为大家推荐几款比较实用的json在线工具供大家参考使用:

在线JSON代码检验、检验、美化、格式化工具:

http://tools.jb51.net/code/json

JSON在线格式化工具:

http://tools.jb51.net/code/jsonformat

在线XML/JSON互相转换工具:

http://tools.jb51.net/code/xmljson

json代码在线格式化/美化/压缩/编辑/转换工具:

http://tools.jb51.net/code/jsoncodeformat

在线json压缩/转义工具:

http://tools.jb51.net/code/json_yasuo_trans

C语言风格/HTML/CSS/json代码格式化美化工具:

http://tools.jb51.net/code/ccode_html_css_json

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新Javascript教程学习
    热门Javascript教程学习
    编程开发子分类