【发布时间】:2014-04-27 08:52:06
【问题描述】:
我正在尝试使用 API。
我想将以下请求存储在对象中:http://api.swissunihockey.ch/rest/v1.0/clubs/655
问题是,对象已初始化,但所有值均为空。
我可以接收数据并以字符串形式生成输出。但是对象的反序列化不起作用。你能帮忙吗?
private static async Task RunAsync()
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://api.swissunihockey.ch/rest/v1.0/clubs/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
try
{
HttpResponseMessage response = await client.GetAsync("615");
var club = await response.Content.ReadAsAsync<Club>();
Console.WriteLine(club.Name);
Console.Read();
}
catch (HttpRequestException e)
{
if (e.Source != null)
{
Console.WriteLine("HttpRequestException source: {0}", e.Source);
}
}
}
}
这是我要存储数据的 Club 类:
class Club
{
public int Id { get; set; }
public string Name { get; set; }
public string Street { get; set; }
public string Zip { get; set; }
public string City { get; set; }
public string Canton { get; set; }
public string Phone { get; set; }
public string Url { get; set; }
}
【问题讨论】:
-
请参阅此link。类似于你的问题
-
谁能解释一下
await client.GetAsync("615");这一行。 615代表什么? -
“615”代表Rest API中对象的ID
标签: c# asp.net json rest asp.net-web-api