Background: Most browsers have implemented native API support for converting json objects to strings. So how to implement it in lower version browsers - such as everyone's favorite IE6?
First run the following method to test the output of JSON.stringify under various circumstances, which will help with the implementation and testing of the following code. The use case may not be complete, welcome to add
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();
The output result is as follows:
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}'
The following is the code implementation of converting json object to string :
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 || {};
//In fact, it is 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 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);
The above code is for practice only. Please forgive me for any redundancy and efficiency issues. If there are any errors, please point them out :)