Json

If you aren't familiar with Json, then you best read up. Here's the documentation for Json.js.

Json.encode

the .encode() method of the Json object will convert an object to a JSON string that can be evaluated back into the object.

Json.encode({apple: 'red', lemon: 'yellow'});
//returns: '{"apple":"red","lemon":"yellow"}'

Json.decode

Json.decode('{"apple":"red","lemon":"yellow"}');
//returns the object again: {apple: 'red', lemon: 'yellow'}
execute this code

A second argument for Json.decode exists to first check that the response is valid Json. This security method ensures that you don't evaluate malicious code:

Json.decode('{"apple":"red","lemon":"yellow"}', true);
//returns the object again: {apple: 'red', lemon: 'yellow'}
execute this code
Json.decode("alert('you just got haxored!')", true);
//returns null
execute this code

Deprecated Methods

  • Json.toString (see Json.encode)
  • Json.evaluate (see Json.decode)

mootorial/05-utilities/02-json.txt · Last modified: 2009/03/05 11:19 by aaron-n