簡體   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