反序列化JSON列表对象
本文关键字:对象 列表 JSON 反序列化 | 更新日期: 2023-09-27 18:08:16
我从一个web请求中收到这个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取决于内容协商和你的应用是否被正确配置,这样就有一个格式化器可以提供消费者请求的输出(在这种情况下是JSON)。