I'm using the Newtonsoft JSON.NET package to deserialize my responses. This is a sample reponse from a call that pulls back available metrics from Omniture.
[
{"rsid":"somersid",
"site_title":"somesitetitle",
"available_metrics":[
{"metric_name":"averagepagedepth","display_name":"Average Page Depth"},
{"metric_name":"averagetimespentonpage","display_name":"Average Time Spent on Page"},
{"metric_name":"averagetimespentonsite","display_name":"Average Time Spent on Site"},
{"metric_name":"averagevisitdepth","display_name":"Average Visit Depth"},
{"metric_name":"customersdaily","display_name":"Daily Unique Customers"},
{"metric_name":"customersloyal","display_name":"Loyal Customers"},
{"metric_name":"customersmonthly","display_name":"Monthly Unique Customers"},
{"metric_name":"customersnew","display_name":"New Customers"},
{"metric_name":"customersquarterly","display_name":"Quarterly Unique Customers"}
]
}
]
For the classes I wanted to deserialize to I at first tried the classes that were generated by svcutil against their schema. It created the classes correctly and they appear to work when deserializing a simple object. But anything with a nested array (like above) was failing with the 'Cannot deserialize JSON array into type' error. Upon closer inspection, the svcutil generated classes were using arrays like this:
public partial class report_suite_metrics {
public string rsid { get; set;} ;
public string site_title { get; set;} ;
public metric[] available_metrics { get; set;} ;
}
public partial class metric {
public string metric_name {get; set;}
public string display_name {get; set;}
}
Maybe JSON.NET is trying to force that array to a generic List I thought. All the JSON.NET examples I saw used List to handle sub-arrays so I created new classes to test using the json2csharp utility (http://json2csharp.com/) and got this:
public class AvailableMetric {
public string metric_name { get; set; }
public string display_name { get; set; }
}
public class RootObject {
public string rsid { get; set; }
public string site_title { get; set; }
public List<AvailableMetric> available_metrics { get; set; }
}
which looks exactly in line with all the JSON.NET examples. But it throws the exact same error :( What gives? Anyone ran into this kind of issue? It seems like a very simple and common case so I was surprised it didn't just work as expected. Thanks
report_suite_metrics[]orreport_suite_metrics? The former should work, the latter will give you the error you are seeing.