天天看點

html5 綁定json資料,使用jquery和html5 post json格式資料

(function($) {

functiontoIntegersAtLease(n)

// Format integers to have at least twodigits.

{

return n < 10 ? '0' + n :n;

}

Date.prototype.toJSON =function(date)

// Yes, it polutes the Date namespace, but we'llallow it here, as

// it's damned usefull.

{

return date.getUTCFullYear() + '-' +

toIntegersAtLease(date.getUTCMonth() + 1) + '-' +

toIntegersAtLease(date.getUTCDate());

};

var escapeable =/["\\\x00-\x1f\x7f-\x9f]/g;

var meta = { // table of charactersubstitutions

'\b': '\\b',

'\t': '\\t',

'\n': '\\n',

'\f': '\\f',

'\r': '\\r',

'"' : '\\"',

'\\': '\\\\'

}

$.quoteString =function(string)

// Places quotes around a string,inteligently.

// If the string contains no control characters,no quote characters, and no

// backslash characters, then we can safely slapsome quotes around it.

// Otherwise we must also replace the offendingcharacters with safe escape

// sequences.

{

if (escapeable.test(string))

{

return '"'+ string.replace(escapeable, function (a)

{

var c = meta[a];

if (typeof c === 'string'){

return c;

}

c = a.charCodeAt();

return '\\u00' + Math.floor(c/ 16).toString(16) + (c % 16).toString(16);

}) +'"'

}

return '"' + string + '"';

}

$.toJSON = function(o,compact)

{

var type = typeof(o);

if (type == "undefined")

return"undefined";

else if (type == "number" || type =="boolean")

return o +"";

else if (o === null)

return"null";

// Is it a string?

if (type == "string")

{

return$.quoteString(o);

}

// Does it have a .toJSON function?

if (type == "object"&& typeof o.toJSON =="function")

returno.toJSON(compact);

// Is it an array?

if (type != "function"&& typeof(o.length) =="number")

{

var ret =[];

for (var i= 0; i < o.length; i++) {

ret.push( $.toJSON(o[i],compact) );

}

if(compact)

return "[" + ret.join(",") +"]";

else

return "[" + ret.join(", ") +"]";

}

// If it's a function, we have to warnsomebody!

if (type == "function") {

throw newTypeError("Unable to convert object of type 'function' tojson.");

}

// It's probably an object, then.

ret = [];

for (var k in o) {

varname;

var type =typeof(k);

if (type== "number")

name = '"' + k + '"';

else if(type == "string")

name =$.quoteString(k);

else

continue; //skip non-string or number keys

val =$.toJSON(o[k], compact);

if(typeof(val) != "string") {

// skip non-serializablevalues

continue;

}

if(compact)

ret.push(name + ":" +val);

else

ret.push(name + ": " +val);

}

return "{" + ret.join(", ") + "}";

}

$.compactJSON =function(o)

{

return $.toJSON(o, true);

}

$.evalJSON =function(src)

// Evals JSON that we know to be safe.

{

return eval_r("(" + src + ")");

}

$.secureEvalJSON =function(src)

// Evals JSON in a way that is *more*secure.

{

var filtered = src;

filtered = filtered.replace(/\\["\\\/bfnrtu]/g,'@');

filtered =filtered.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,']');

filtered =filtered.replace(/(?:^|:|,)(?:\s*\[)+/g, '');

if (/^[\],:{}\s]*$/.test(filtered))

returneval_r("(" + src + ")");

else

throw newSyntaxError("Error parsing JSON, source is not valid.");

}

})(jQuery);