Autor | Zpráva | ||
---|---|---|---|
Charlie Profil |
#1 · Zasláno: 3. 9. 2007, 18:11:20
Zdravím,
je možné pomocí JavaScriptu serializovat nějaký objekt, uložit ho do cookie a následně deserializovat? Pokud ano, jak? Mockrát díky |
||
centi Profil |
#2 · Zasláno: 3. 9. 2007, 19:54:55
var serial = obj.toSource();
Môžeš ale 3x hádať, v ktorom browsri to nefunguje. Prípadne sa môžeš pokúsiť vysekať metódu YAHOO.Tools.JSONEncode z http://blog.davglass.com/files/yui/tools/tools.js (Tools pre Yahoo UI knižnice od Dava Glassa). |
||
Charlie Profil |
#3 · Zasláno: 3. 9. 2007, 20:41:10
centi, o .toSource() jsem věděl, taky se mi to velmi zamlouvalo, ale... :)
Moc díky za tip na pomůcku v Yahoo UI. Chtěl jsem to už předtím řešit přeš JSON, ale ještě jsem si říkal, že to nějak zkusím bez toho. Ale očividně se to na to hodí. |
||
centi Profil |
#4 · Zasláno: 3. 9. 2007, 21:04:29
Charlie
No hodí sa na to už len preto, že samotné JSON vlastne je objekt :) JavaScript Object Notation. |
||
Časová prodleva: 1 měsíc
|
|||
Petroff Profil |
#5 · Zasláno: 7. 10. 2007, 11:35:09
var Json = {
/* Property: toString Converts an object to a string, to be passed in server-side scripts as a parameter. Although its not normal usage for this class, this method can also be used to convert functions and arrays to strings. Arguments: obj - the object to convert to string Returns: A json string Example: (start code) Json.toString({apple: 'red', lemon: 'yellow'}); '{"apple":"red","lemon":"yellow"}' (end) */ toString: function(obj){ switch($type(obj)){ case 'string': return '"' + obj.replace(/(["\])/g, '\$1') + '"'; case 'array': return '[' + obj.map(Json.toString).join(',') + ']'; case 'object': var string = []; for (var property in obj) string.push(Json.toString(property) + ':' + Json.toString(obj[property])); return '{' + string.join(',') + '}'; case 'number': if (isFinite(obj)) break; case false: return 'null'; } return String(obj); }, /* Property: evaluate converts a json string to an javascript Object. Arguments: str - the string to evaluate. if its not a string, it returns false. secure - optionally, performs syntax check on json string. Defaults to false. Credits: Json test regexp is by Douglas Crockford <http://crockford.org>. Example: >var myObject = Json.evaluate('{"apple":"red","lemon":"yellow"}'); >//myObject will become {apple: 'red', lemon: 'yellow'} */ evaluate: function(str, secure){ return (($type(str) != 'string') || (secure && !str.test(/^("(\.|[^"\ ])*?"|[,:{}[]0-9.-+Eaeflnr-u ])+?$/))) ? null : eval('(' + str + ')'); } }; |
||
Časová prodleva: 16 let
|
0