Skip to content Skip to sidebar Skip to footer

Deserializing Json As A Generic List

I have a filter string as shown in the below format: {'groupOp':'AND','rules':[{'field':'FName','op':'bw','data':'te'}]} I need to deserialize this as a Generic list of items. Can

Solution 1:

Have a look at JSON.NET. It allows you to do things like:

JObjecto=newJObject(
  newJProperty("Name", "John Smith"),
  newJProperty("BirthDate", newDateTime(1983, 3, 20))
  );

JsonSerializerserializer=newJsonSerializer();
Personp= (Person)serializer.Deserialize(newJTokenReader(o), typeof(Person));

Console.WriteLine(p.Name);
// John Smith

Source: the documentation.

Solution 2:

Try using the JavaScriptSerializer class like so:

Deserialization code

using System.Web.Script.Serialization;

...

string json ="{\"groupOp\":\"AND\",\"rules\":[{\"field\":\"FName\",\"op\":\"bw\",\"data\":\"te\"}]}";
JavaScriptSerializer serializer = new JavaScriptSerializer();
Filter filter = (Filter)serializer.Deserialize<Filter>(json);

Classes

publicclassFilter
{
    publicstring GroupOp { get; set; }
    public List<Rule> Rules { get; set; }

    publicFilter()
    {
        Rules = new List<Rule>();
    }
}

publicclassRule
{
    publicstring Field { get; set; }
    publicstring Op { get; set; }
    publicstring Data { get; set; }
}

Post a Comment for "Deserializing Json As A Generic List"