我想在我的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; }
}
你甚至可以進一步簡化這個'output = await response.Content.ReadAsAsync ();' – Nkosi