09 May 2011

C# - Parse JSON

Parsing JSON to strongly types objects is real easy in C#.
You just need to have the objects as they are represented in JSON, like these:

public class GridFilter {
  public string GroupOp { get; set; }
  public IEnumerable Rules { get; set; }
}

public class GridFilterRule {
  public string Field { get; set; }
  public string Op { get; set; }
  public string Data { get; set; }
}

Then all you need to do is this:

GridFilter filter = new JavaScriptSerializer().Deserialize(filters);

And your object structure will be created and filled from a JSON string.

No comments:

Post a Comment