0

I have a cookie that is saved via jQuery plugin on which the value is JSON.stringified object.

I need to read and decode this cookie in C#. Although I have successfully read the cookie, I had no success on converting it to an object.

Any clue is greatly appreciated

MrCADman
  • 99
  • 1
  • 8
  • There are already many questions about how to deserialize JSON in c#, for instance [Deserialize JSON with C#](https://stackoverflow.com/questions/7895105), or [How to Deserialize JSON data?](https://stackoverflow.com/questions/18242429) or [How can I parse JSON with C#?](https://stackoverflow.com/questions/6620165), or [deserializing JSON to .net object using NewtonSoft (or linq to json maybe?)](https://stackoverflow.com/questions/4749639). Please be more specific about your requirements. – dbc May 12 '16 at 21:50

3 Answers3

0

A fun way to do it is via dynamic dynamic person = Json.Decode(jsonString);

you will be blind ind the intellisesne way, but aslong as you knwo the structure then you can just dot your way around like this

dynamic person = Json.Decode(jsonString);
string firstname = person.Firstname;
int age = person.Age;
//etc.

There is alot of libraries Newtonsoft's Json.NET or System.Web.Helpers are tho good ones.

Json.NET syntax is JsonConvert.DeserializeObject(jsonString)

System.Web.Helpers syntax is as mentioned Json.Decode(jsonString)

and if you have the model for the recieving object then jsut add it to the generic like this

Person person = JsonConvert.DeserializeObject<Product>(jsonString);
Thomas Andreè Wang
  • 3,379
  • 6
  • 37
  • 53
0

Try

JObject jobj = JObject.Parse(target);     
Fan Jin
  • 2,412
  • 17
  • 25
0

You can use

new JavaScriptSerializer().Deserialize<YorObjectClass>(jsonString)

see details about JavaScriptSerializer