JSON to string in Prototype
21,193
Sure there is: Prototype JSON
var data = {name: 'Violet', occupation: 'character', age: 25 };
var myString = Object.toJSON(data);
// myString = '{"name": "Violet", "occupation": "character", "age": 25}'
Then shove myString into your cookie
Author by
Admin
Updated on December 30, 2020Comments
-
Admin over 3 years
The people on this website seem to know everything so I figured I would ask this just in case:
Is there a method/function in prototype that converts a JSON object to a string that you can store in a cookie?
If not,..i'll just use another external library.
Thanks, Andrww
-
Greg over 15 yearsPlain Javascript can go from JSON string to object, but doesn't have a function to JSON-encode an object to a string
-
Greg over 15 yearsObject is part of javascript - it must be something else that's not working. Either Prototype isn't loaded properly, or data (or whatever you called your argument) doesn't exist
-
orip over 15 yearsJavaScript doesn't have JSON functionality (neither encoding, nor safe decoding). Firefox 3.1 and IE 8 implemented Douglas Crockford's "json2" API, but for everything else you need a library.
-
P Arrayah over 15 yearsThere's four libraries available at json.org for the functionality and from what I've read and understood JSON support already is in most mainline browsers (with the natural exception of IE7 of course), I'm a bit confused by this, for me this is like asking if I should drink water if I'm thirsty.
-
Greg over 15 yearsJavascript natively accepts json, e.g. var foo = {}. What it doesn't do is string-to-object ('{}') or object-to-string (foo -> '{}')
-
Greg over 15 yearsAlso it's not "safe" as you can do var foo = {"blah":someFunction()} - of course, this can be a very useful feature as well
-
Admin over 15 yearshm...alright i'll take your word for it, prototype has to be loaded cuz i use all the $() and scriptaculous etc. ....so i don't know what it could be. all well, JSON.js it is.,...thanks for the answers
-
Admin over 15 yearsupon further inspection if I put the code inside a function it works! ...(i was just testing it outside any function)....:)
-
Admin over 15 yearsya that's probably it for sure actually,...i'm using symfony so that probably loads the prototype library after the initial java. the world makes sense again. thanks.