繁体   English   中英

反序列化JSON List对象

[英]Deserializing JSON List object

我从网络请求中收到此JSON对象:

"\"[{\\\"EventId\\\":25,\\\"StartDate\\\":\\\"2014-06-12T12:00:00\\\",\\\"EndDate\\\":\\\"2014-06-17T12:00:00\\\",\\\"Title\\\":\\\"Test\\\",\\\"Description\\\":\\\"desc\\\",\\\"Teaser\\\":\\\"teaser\\\",\\\"PhoneNumber\\\":null,\\\"Email\\\":null,\\\"AddressOne\\\":null,\\\"AddressTwo\\\":null,\\\"City\\\":null,\\\"State\\\":null,\\\"ZipCode\\\":null,\\\"Country\\\":null,\\\"RegistrationUrl\\\":null,\\\"CreatedBy\\\":\\\"c216cd34-6a3d-4f38-950b-ea3383a30a64\\\"},{\\\"EventId\\\":25,\\\"StartDate\\\":\\\"2014-06-13T12:00:00\\\",\\\"EndDate\\\":\\\"2014-06-18T12:00:00\\\",\\\"Title\\\":\\\"Test\\\",\\\"Description\\\":\\\"desc\\\",\\\"Teaser\\\":\\\"teaser\\\",\\\"PhoneNumber\\\":null,\\\"Email\\\":null,\\\"AddressOne\\\":null,\\\"AddressTwo\\\":null,\\\"City\\\":null,\\\"State\\\":null,\\\"ZipCode\\\":null,\\\"Country\\\":null,\\\"RegistrationUrl\\\":null,\\\"CreatedBy\\\":\\\"c216cd34-6a3d-4f38-950b-ea3383a30a64\\\"},{\\\"EventId\\\":25,\\\"StartDate\\\":\\\"2014-06-14T12:00:00\\\",\\\"EndDate\\\":\\\"2014-06-19T12:00:00\\\",\\\"Title\\\":\\\"Test\\\",\\\"Description\\\":\\\"desc\\\",\\\"Teaser\\\":\\\"teaser\\\",\\\"PhoneNumber\\\":null,\\\"Email\\\":null,\\\"AddressOne\\\":null,\\\"AddressTwo\\\":null,\\\"City\\\":null,\\\"State\\\":null,\\\"ZipCode\\\":null,\\\"Country\\\":null,\\\"RegistrationUrl\\\":null,\\\"CreatedBy\\\":\\\"c216cd34-6a3d-4f38-950b-ea3383a30a64\\\"},{\\\"EventId\\\":25,\\\"StartDate\\\":\\\"2014-06-15T12:00:00\\\",\\\"EndDate\\\":\\\"2014-06-20T12:00:00\\\",\\\"Title\\\":\\\"Test\\\",\\\"Description\\\":\\\"desc\\\",\\\"Teaser\\\":\\\"teaser\\\",\\\"PhoneNumber\\\":null,\\\"Email\\\":null,\\\"AddressOne\\\":null,\\\"AddressTwo\\\":null,\\\"City\\\":null,\\\"State\\\":null,\\\"ZipCode\\\":null,\\\"Country\\\":null,\\\"RegistrationUrl\\\":null,\\\"CreatedBy\\\":\\\"c216cd34-6a3d-4f38-950b-ea3383a30a64\\\"},{\\\"EventId\\\":25,\\\"StartDate\\\":\\\"2014-06-16T12:00:00\\\",\\\"EndDate\\\":\\\"2014-06-21T12:00:00\\\",\\\"Title\\\":\\\"Test\\\",\\\"Description\\\":\\\"desc\\\",\\\"Teaser\\\":\\\"teaser\\\",\\\"PhoneNumber\\\":null,\\\"Email\\\":null,\\\"AddressOne\\\":null,\\\"AddressTwo\\\":null,\\\"City\\\":null,\\\"State\\\":null,\\\"ZipCode\\\":null,\\\"Country\\\":null,\\\"RegistrationUrl\\\":null,\\\"CreatedBy\\\":\\\"c216cd34-6a3d-4f38-950b-ea3383a30a64\\\"}]\""

我有一个匹配此类型的对象称为EventView,并将其反序列化为这样的对象:

string result = "";
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
      StreamReader reader = new StreamReader(response.GetResponseStream());

      result = reader.ReadToEnd();
}
JavaScriptSerializer jss = new JavaScriptSerializer();
JsonEventView ev = jss.Deserialize<JsonEventView>(result);



public class JsonEventView
{
    public List<EventView> results { get; set; }
}

但是,每次尝试反序列化时都会出现错误。

Cannot convert object of type 'System.String' to type 'EventManagerService.JsonEventView'

Web API为您处理序列化。 因此, 无需手动将对象序列化为JSON。

return Request.CreateResponse(HttpStatusCode.OK, Active);

当然,Web API能够返回JSON取决于Content Negotiation内容协商),并且您的应用程序已正确配置,因此有一个格式化程序可以为使用者提供输出请求(在这种情况下为JSON)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM