vivanov vivanov - 6 months ago 103
C# Question

C# deserialize JSON using JavaScriptSerializer.DeserializeObject

I am not a C# programmer, but rather playing with the language from time to time. I wonder, if I have a JSON string which I want to deserialize using JavaScriptSerializer.DeserializeObject, how could I do that. For instance, if I have a JSON:

"Name": "col_name2",
"Value": [
"From": 100,
"To": 200
"From": 100,
"To": 200

And I have that JSON string in a variable called

using System.Web.Script.Serialization;

JavaScriptSerializer jss = new JavaScriptSerializer();
Object json = jss.DeserializeObject(sJson);

and now how do I use this
Object json

Note: I already know how to do it using

Answer Source

Look at this post for Davids Answer:

Deserialize json object into dynamic object using

You can put it into a dynamic (underlying type is JObject) Then you can than access the information like this:

JavaScriptSerializer jss = new JavaScriptSerializer();
dynamic json = jss.DeserializeObject(sJson);
Console.WriteLine(json.Name); // col_name2

I would prefer create a data transfer object (DTO) represent your JSON Structure as a c# class.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download