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

Share:
21,193
Admin
Author by

Admin

Updated on December 30, 2020

Comments

  • Admin
    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
    Greg over 15 years
    Plain Javascript can go from JSON string to object, but doesn't have a function to JSON-encode an object to a string
  • Greg
    Greg over 15 years
    Object 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
    orip over 15 years
    JavaScript 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
    P Arrayah over 15 years
    There'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
    Greg over 15 years
    Javascript natively accepts json, e.g. var foo = {}. What it doesn't do is string-to-object ('{}') or object-to-string (foo -> '{}')
  • Greg
    Greg over 15 years
    Also it's not "safe" as you can do var foo = {"blah":someFunction()} - of course, this can be a very useful feature as well
  • Admin
    Admin over 15 years
    hm...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
    Admin over 15 years
    upon further inspection if I put the code inside a function it works! ...(i was just testing it outside any function)....:)
  • Admin
    Admin over 15 years
    ya 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.