tags:

views:

333

answers:

1

Hi,

I'm trying to use the JQuery cookies library http://code.google.com/p/cookies/ But am not able to set key value pairs within the cookie. I can't see how in the documentation, does anyone know how?

Thanks

A: 

Check out the JSON example in the link you referenced.

//JSON 
var jimData = {id: 1, name: 'jim'}; 
$.cookies.set( 'userdata', jimData ); //A cookie by the name 'userdata' now exists with a serialized copy of jimData 

var userData = $.cookies.get( 'userdata' ); //A variable named 'userData' now holds the unserialized object--should be identical to the variable 'jimData' 

So, userData should now have properties id with value 1 and name with value 'jim'.

tvanfosson
I saw that but will I be able to retrieve the object as a key value pair from c#?
nav
I would rather not have to decode a JSON object in c# as this does not seem straightforward
nav
Have you looked at the DataContractJsonSerializer, http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer.aspx? If you had a class with the correct properties onto which the cookie would map the code should be trivial.
tvanfosson
That looks cool do you know which .net library this belong to?JavaScriptConvert.DeserializeObject
nav
Are you thinking of JSON.Net: http://james.newtonking.com/projects/json-net.aspx? The DataContractJsonSerializer is in System.Runtime.Serialization. I haven't used JSON.Net.
tvanfosson