在jQuery中序列化/反序列化 [英] serialize/unserialize in jQuery
问题描述
是否有像jQuery中序列化/反序列化PHP函数的东西?
Is there something like serialize/unserialize PHP functions in jQuery?
这些函数返回一个数组或一个对象的字符串表示,可以将其解码回数组/ object。
These functions return a string representations of an array or an object which can than be decoded back into array/object.
http://sk2.php。 net / serialize
推荐答案
jQuery的serialize / serializeArray仅适用于表单元素。我认为你正在寻找更通用的东西:
jQuery's serialize/serializeArray only works for form elements. I think you're looking for something more generic like this:
http://code.google.com/p/jquery-json/
这插件使得转换为JSON变得简单:
This plugin makes it simple to convert to and from JSON:
var thing = {plugin: 'jquery-json', version: 2.2};
var encoded = $.toJSON(thing);
//'{"plugin":"jquery-json","version":2.2}'
var name = $.evalJSON(encoded).plugin;
//"jquery-json"
var version = $.evalJSON(encoded).version;
// 2.2
大多数人问我为什么要卖
呢?这样的事情,令人难以置信。由于
eval(),Javascript使其相对
易于转换为JSON,但转换为JSON是
,据说是边缘要求。
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.
这个插件在$或jQuery对象上公开了四个新函数
:
This plugin exposes four new functions onto the $, or jQuery object:
-
toJSON
:将javascript对象,数字,字符串或arry序列化为JSON。 -
evalJSON
:转换自JSON到Javascript,很快,而且很简单。 -
secureEvalJSON
:从JSON转换为Javascript,但在检查时是否这样做源实际上是JSON,而不是引入的其他Javascript语句。 -
quoteString
:在字符串周围放置引号,并且明智地转义任何引号,反斜杠或控制字符。
toJSON
: Serializes a javascript object, number, string, or arry 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 inteligently escapes any quote, backslash, or control characters.
这篇关于在jQuery中序列化/反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!