2014-06-11 3 views
0

나는 웹 요청에서이 JSON 객체가 나타납니다역 직렬화 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; } 
} 

그러나 deserialize하려고 할 때마다 오류가 발생합니다.

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

JSON이 이중 인코딩 된 것처럼 보입니다. 즉, JSON 객체의 JSON 문자열이있는 것 같습니다. – univerio

+0

@univerio API에서 반환 한 내용은 다음과 같습니다. return Request.CreateResponse (HttpStatusCode.OK, JsonConvert.SerializeObject (Active)); 나는 그것을 바꿔야합니까? –

+0

아마도 답을 요구하는 코드를 추가하십시오. –

답변

1

웹 API가 일련 번호를 처리합니다. 그래서 은 객체를 JSON으로 수동으로 직렬화 할 필요가 없습니다. 출력을 제공 할 수있는 formatter가되도록, 물론

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

, JSON을 반환 할 수있는 웹 API는 Content Negotiation에 달려과 응용 프로그램은 제대로 구성되고 소비자의 요청 (이 경우 JSON).