background : Most browsers have implemented the native API support for JSON object-turn strings, so how do you do that in a lower browser browser, such as your favorite ie6--?
First, run the following methods to test the output of json.stringify in various situations, which will help you implement the code below and test it. Use cases are not complete, welcome to add
Copy Code code as follows:
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 results are as follows:
Copy Code code 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 the JSON object's spin string:
Copy Code code as follows:
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] ';}
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 | | {};
Actually, it's 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 to 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);
The above code only for practicing, if there is redundancy and efficiency issues please forgive me. If there is a mistake, please help point out:
PS: About JSON operation, here we recommend a few more practical JSON online tools for your reference to use:
Online JSON code inspection, inspection, landscaping, formatting tools:
Http://tools.jb51.net/code/json
JSON Online formatting tool:
Http://tools.jb51.net/code/jsonformat
Online Xml/json Mutual Conversion tool:
Http://tools.jb51.net/code/xmljson
JSON code online Format/beautify/compress/edit/Convert tools:
Http://tools.jb51.net/code/jsoncodeformat
Online JSON compression/escape tool:
Http://tools.jb51.net/code/json_yasuo_trans
C Language Style/html/css/json code formatting landscaping Tools:
Http://tools.jb51.net/code/ccode_html_css_json