Today, I received an email from someone who was not able to use my JavascriptSerializer example to parse out some JSON text. It took me several minutes to realize what I was doing wrong. Here is the JSON text in question:

{
"ListFilterControl": [
 { "filterControlObject": {
 "DisplayFieldID": "20",
 "ControlDisplayName": "Country",
 "ControlType": "TextBox"
 }
 },
 {
 "filterControlObject": {
 "DisplayFieldID": "21",
 "ControlDisplayName": "automobile",
 "ControlType": "ComboBox"
 }
 }]
}

My problem was that the topmost object is an array. In my examples, the topmost object was an object that was NOT an array. Once I realize this, I was able to re-write the classes so that I could parse out the JSON text.

Here are the classes that I used to parse out this text. I hope you find this useful:

/*------------------------------------------------
 * Classes needed for deserialization
 * ----------------------------------------------*/
 /// <summary>
 /// Outermost wrapper
 /// </summary>
 public class jListFilterControlWrapper
 {
 public jfilterControlObject[] ListFilterControl; // Array of filterControlObjects
 }
 /// <summary>
 /// The filterControlObject items (there is an array of these objects)
 /// </summary>
 public class jfilterControlObject
 {
 public jFilterControlProperties filterControlObject; // A filterControlObject contains a FilterControlProperties object
 }
 /// <summary>
 /// The properties of the filterControlObject
 /// </summary>
 public class jFilterControlProperties
 { // The specific properties you want to use
 public string DisplayFieldID { get; set; }
 public string ControlDisplayName { get; set; }
 public string ControlType { get; set; }
 }

One Reply to “A JavascriptSerializer twist”

  1. I’m looking to parse JSON data that has the following format:

    [{"event":"updateTime","data":"2013-09-14 15:45:10.374"}]

    So it is a top-level array that doesn’t have a key. Thoughts on how to deserialize this?

Leave a Reply

Your email address will not be published. Required fields are marked *