首頁  >  文章  >  web前端  >  json物件轉字串如何實作_javascript技巧

json物件轉字串如何實作_javascript技巧

WBOY
WBOY原創
2016-05-16 17:47:301052瀏覽
背景:大部分瀏覽器已經實現了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) 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 . 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(object Unobject(obj){object(obj){object(obj){object(obj){object(obj){object(obj){object(obj){object(obj){object(obj){object(obj){object(obj){object(obj){object(obj){object(obj){object(obj){object(obj){object(obj){object(obj){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; ivar 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}
}
}
return left}
}
}
return left}
}
}
return left}
}
}
return left}
}
}
return left}
}
}
return left}
}
}
return left} } 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); 以上程式碼僅為練手用,如有冗餘及效率問題敬請見諒。如有錯誤則請幫忙指出 :)
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn