Saving a complex JSON object in a cookie by using jQuery (demo included in PHP)
Today, I spent almost three hours to test and implement a mechanism to save a complex JSON object and get it to render in a HTML output,, for this reason I would like to explain what was the best easiest way that I found for saving my object.
//A cookie by the name 'userSettings' now exists with a serialized copy of $userSettings
$.cookies.set( 'userSettings', <?php echo $jsonObject; ?> );
//A variable named 'userSettings' now holds the unserialized object, it should be identical to the PHP variable 'userSettings'
var userSettings = $.cookies.get( 'userSettings' );
// Do something with the values read from cookie
To see the saved object please use Firebug (in cookie tab). See next images like a demo of this.
Figure 1. Saving JSON object
Figure 2. Getting JSON object and verifying in Firebug console