JS implementation of JSON data serialization (compatible with IE6 or more browsers)

Source: Internet
Author: User

/**
* Add a serialization method to JSON data,
* Primarily for IE6, 7 browsers that do not support JSON objects
*/

var xue = Xue | | {};
Xue.json = Xue.json | | {};

xue.json.stringify = function (obj) {
If the ie8+ browser (Ff,chrome,safari supports JSON objects), use Json.stringify () to serialize
if (window. JSON) {
return json.stringify (obj);
}
var t = typeof (obj);
if (t! = "Object" | | obj = = = null) {
Simple data type
if (t = = "string") obj = ' "' + obj + '";
return String (obj);
} else {
Recurse Array or Object
var n, V, json = [],
arr = (obj && obj.constructor = = Array);

Fix.
var self = Arguments.callee;

for (n in obj) {
v = obj[n];
t = typeof (V);
if (Obj.hasownproperty (n)) {
if (t = = "string") v = ' "' + V + '";
else if (t = = "Object" && v!== null)
v = jquery.stringify (v);
v = self (v);
Json.push (arr? "": ' "' + N + '": ') + String (v));
}
}
Return (arr?) "[": "{") + String (JSON) + (arr. "]": "}");
}
};
/*
* Method:parse (JSON string)
* Return:js Native value
*/
Xue.json.parse = function (jsonstring) {
if (window. JSON) {
return window. Json.parse (s);
}
Using the Parsejson (s) method of jquery
Return $.parsejson (jsonstring);
};

How to use:

var _json = {

"js_58": [' a ', ' B '],

"js_39": [' a ', ' B ', ' C '],

"js_68": [' a ']

}

To serialize a JSON object _json:

Xue.json.stringify (_json);

Note: If the AJAX parameter is a JSON data object, it is best to serialize the JSON object first

JS implementation of JSON data serialization (compatible with IE6 or more browsers)

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.