2014-05-10 113 views
12

我想在我的mvc項目中使用ReadAsAsync().net 4.0。結果爲空。使用ReadAsAsync <T>()反序列化複雜的Json對象

如果我進入URI地址欄,結果在鉻作爲(標籤名稱更改):

<ns2:MyListResponse xmlns:ns2="blablabla"> 
    <customerSessionId>xxcustomerSessionIdxx</customerSessionId> 
    <numberOfRecordsRequested>0</numberOfRecordsRequested> 
    <moreResultsAvailable>false</moreResultsAvailable> 
    <MyList size="1" activePropertyCount="1"> 
    <MySummary order="0"> 
     <id>1234</id> 
     <name>...</name> 
     . 
     . 
    </MySummary> 
    </MyList> 
</ns2:MyListResponse> 

如果我使用的語句代碼:

using (var client = new HttpClient()) 
{ 
    var response = client.GetAsync(apiUri).Result; 
    var message = response.Content.ReadAsStringAsync().Result; 

    var result1 = JsonConvert.DeserializeObject<MyListResponse>(message); 
    var result2 = response.Content.ReadAsAsync<MyListResponse>().Result; 
} 

消息來字符串格式爲"{\"MyListResponse\":{\"customerSessionId\"...}",它對應於json對象:

{"MyListResponse": 
    {"customerSessionId":"xxcustomerSessionIdxx", 
    "numberOfRecordsRequested":0, 
    "moreResultsAvailable":false, 
    "MyList": 
     {"@size":"1", 
     "@activePropertyCount":"1", 
     "MySummary": 
      {"@order":"0", 
      "id":1234, 
      "name":"...", 
      . 
      . 
      } 
     } 
    } 
} 

,result1和result2的屬性爲空或默認值。類定義如下。我想將內容作爲對象閱讀,但我不能。你有什麼建議來解決這個問題?我究竟做錯了什麼?提前致謝。

public class MySummary 
{ 
    public int @Order { get; set; } 
    public string Id { get; set; } 
    public string Name { get; set; } 
    . 
    . 
} 

public class MyList 
{ 
    public int @Size { get; set; } 
    public int @ActivePropertyCount { get; set; } 
    public MySummary MySummary{ get; set; } 
} 

public class MyListResponse 
{ 
    public string CustomerSessionId { get; set; } 
    public int NumberOfRecordsRequested { get; set; } 
    public bool MoreResultsAvailable { get; set; } 
    public MyList MyList { get; set; } 
} 

回答

7

我定義了一個新的類爲:

public class ResponseWrapper 
{ 
    public MyListResponse MyListResponse { get; set; } 
} 

然後我用這個包裝帶,

var result1 = JsonConvert.DeserializeObject<ResponseWrapper>(message); 
var result2 = response.Content.ReadAsAsync<ResponseWrapper>().Result; 

然後它的工作。我只需要MySummary對象,但我應該編寫更多的類來使其工作。

6

閱讀您的解決方案後,我想出了一個並不需要一個額外的類:

private static async Task<U> Execute<U>(HttpClient client, string path) 
    { 
     U output = default(U); 

     HttpResponseMessage response = await client.GetAsync(path); 

     if (response.IsSuccessStatusCode) 
     { 
      var jsonAsString = await response.Content.ReadAsStringAsync(); 
      output = JsonConvert.DeserializeObject<U>(jsonAsString); 
     } 
     else 
     { 
      throw new ApplicationException(string.Format("Response message is not OK. Issues in action: {0}", path)); 
     } 

     return output; 
    } 
+1

你甚至可以進一步簡化這個'output = await response.Content.ReadAsAsync ();' – Nkosi

3

對於未來的讀者着想,我認爲正確的做法是使用ReadAsAsync重載需要​​,並提供一個具有與服務器上序列化相同的設置的格式化程序。這應該解決它。

0

可以直接在客戶端ReadAsAsync和MyListResponse中使用(因此無需ResponseWrapper)。爲此,您可以在「apiuri」的操作合同中定義「BodyStyle = WebMessageBodyStyle.Bare」,而不是「BodyStyle = WebMessageBodyStyle.Wrapped」(服務器端,即服務合同)。