2015-01-13 48 views
0

我正在使用REST服務從Db獲取數據。 在我的課堂上,我擁有數據類型TimeSpan的屬性「UploadTime」。 的時間跨度是基於日期之間並在響應之差計算,我是個越來越響應爲:在休息服務序列化中面臨Timespan問題

ArrayOfUploadUI xmlns="http://schemas.datacontract.org/2004/07/NMS.ApplicationService.HIM.Objects" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
<UploadUI> 
    <UID>1</UID> 
    <DateCompleted>2015-01-08T10:46:25.25</DateCompleted> 
    <DateNotified>2015-01-07T10:46:25.25</DateNotified> 
    <DateDictationStartTime i:nil="true" /> 
    <DateDictationEndTime i:nil="true" /> 
    <UploadTime>P1D</UploadTime> 
    <ExistsOnBackend>false</ExistsOnBackend> 
    </UploadUI> 
    </ArrayOfUploadUI> 

。 我得到以下異常,而試圖獲取數據:

using (HttpResponseMessage response = await _HttpClient(session).GetAsync(apiUrl)) 
      { 
       if (response.IsSuccessStatusCode) 
        result = await response.Content.ReadAsAsync<T>(); 
       else 
        await _HandleInvalidResponseAsync(response); 
      } 

我得到的問題在「response.Content.ReadAsAsync();」。

錯誤是:

Error converting value "P1D" to type 'System.Nullable`1[System.TimeSpan]'. Path '[0].UploadTime', line 1, position 518. 
    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType). 

我一派,我才知道,WCF serilaizing從 「時間跨度」 數據類型 「1:0:0:00」 至 「P1D」。

我的問題是如何在響應級別反序列化這個。

回答

0

我已經完成源代碼級日期的格式化並且問題得到解決。

JsonSerializerSettings dateFormatSettings = new JsonSerializerSettings 
      { 
       DateFormatHandling = DateFormatHandling.MicrosoftDateFormat 
      }; 
      string jsonNMSPlatformObject = JsonConvert.SerializeObject(nmsPlatformObject,dateFormatSettings); 
      using (HttpContent httpContent = new StringContent(jsonNMSPlatformObject)) 
      { 
       httpContent.Headers.ContentType = new MediaTypeHeaderValue(JsonMedaType); 
       var request = new HttpRequestMessage(HttpMethod.Delete, apiUrl); 
       request.Content = httpContent; 
       using (HttpResponseMessage response = await _HttpClient(session).SendAsync(request)) 
       { 
        if (response.IsSuccessStatusCode) 
         return; 
        else 
         await _HandleInvalidResponseAsync(response, jsonNMSPlatformObject); 
       } 
      }