一个jquery的json插件,可以实现json的编码和解码
This plugin makes it simple to convert to and from JSON:
var thing = {plugin: 'jquery-json', version: 1.3};
var encoded = $.toJSON(thing); // '{“plugin”: “jquery-json”, “version”: 1.3}'
var name = $.evalJSON(encoded).plugin; // “jquery-json”
var version = $.evalJSON(encoded).version; // 1.3
This plugin exposes five new functions onto the $, or jQuery object:
* toJSON: Serializes a javascript object, number, string, or arry into JSON.
* compactJSON: Serializes as toJSON, but takes out some spaces, making the result that much shorter.
* evalJSON: Converts from JSON to Javascript, quickly, and is trivial.
* secureEvalJSON: Converts from JSON to Javascript, but does so while checking to see if the source is actually JSON, and not with other Javascript statements thrown in.
* quoteString: Places quotes around a string, and inteligently escapes any quote, backslash, or control characters.
ajax例子:
$(document).ready(function(){
var data = new Object();
data.hello = “Hello”;
data.world = 'World';
data.worked = ” it worked “;
data.somebool = true;
data.array = new Array(“he\”ll\”o”, '”World”');
var dataString = $.toJSON(data);
$.post('phpfile.php', {data: dataString}, function(res){