解析JSON响应的JavaScriptserializer异常

时间:2012-05-04 15:56:21

标签: asp.net json deserialization

我正在从Citrix的Web服务解析JSON响应。响应看起来像这样

[{\"webinarKey\":123456,\"subject\":\"Subject\",\"description\":\"Webinar Description. \",\"organizerKey\":123456,\"times\":[{\"startTime\":\"2012-05-08T16:00:00Z\",\"endTime\":\"2012-05-08T17:00:00Z\"}],\"timeZone\":\"America/New_York\"}]

我手动编辑该字符串以删除识别信息,因此如果缺少引号或任何不相关的内容。

我在SO上按照这个答案的例子,但仍然遇到错误。

Deserializing JSON result with Json & JavaScriptSerializer

public class Webinars {
    public string webinarKey;
    public string subject;
    public string description;
    public string organizerKey;
    public WebinarTimes[] times;
    public string timeZone;
}

public class WebinarTimes {
    public string startTime;
    public string endTime;
}

JavaScriptSerializer jss = new JavaScriptSerializer();
var foo = jss.Deserialize<Webinars>(JSON);

我收到以下错误:Type 'Web.Site.Webinars' is not supported for deserialization of an array.

1 个答案:

答案 0 :(得分:2)

您必须使用IList<Webinars>代替Webinars

var foo = jss.Deserialize<IList<Webinars>>(JSON);