How to convert a json object to a string

Source: Internet
Author: User

Background: Most browsers have implemented native api support for converting json objects to strings. How can this be achieved in earlier browser versions, such as Internet Explorer 6, which is your favorite?
First, run the following method to test the output of JSON. stringify in various cases, which helps to implement and test the following code. The use cases are not necessarily complete.
Copy codeThe Code is 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]),
"JSON. stringify ([undefined, undefined])": JSON. stringify ([undefined, undefined]),
"JSON. stringify ({name: 'chingp', age: 24, u: undefined}) ": JSON. stringify ({name: 'chingp', 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:
Copy codeThe Code is as follows:
JSON. stringify (undefined): undefined
JSON. stringify (null): 'null'
JSON. stringify (123): '20140901'
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: 'chingp', age: 24, u: undefined}): '{"name": "chyingp", "age": 24 }'

The code implementation for converting a json object to a string is as follows:
Copy codeThe Code is 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 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 );

The above code is only for hands-on use. If you have any redundancy or efficiency problems, please forgive me. In case of any errors, please help us to point out :)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.