当前位置: 首页 > 工具软件 > jquery-json > 使用案例 >

jquery-json插件

孟佑运
2023-12-01

This plugin makes it simple to convert to and from JSON:

var thing = { plugin: 'jquery-json', version: 2.4 };

var encoded = $.toJSON( thing );
// '{"plugin":"jquery-json","version":2.4}'
var name = $.evalJSON( encoded ).plugin;
// "jquery-json"
var version = $.evalJSON(encoded).version;
// 2.4

Most people asked me why I would want to do such a thing, which boggles my mind. Javascript makes it relatively easy to convert from JSON, thanks to eval(), but converting to JSON is supposedly an edge requirement.

This plugin exposes four new methods onto the (root) jQuery object:

  • toJSON: Serializes a javascript object, number, string, or array into JSON.
  • 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 intelligently escapes any quote, backslash, or control characters.

 类似资料: