返回HttpResponseMessage中的内容并映射到POCO



我正试图从另一个api中使用我的api,所以我使用的是HttpClient。我的代码看起来如下:

public async Task<List<TeamName>> GetIpAddressDetails()
{
HttpClient client = new HttpClient();
try
{
HttpResponseMessage httpResponse = await client.GetAsync(adminURL);
var content = await httpResponse.Content.ReadAsStringAsync();
var resp = new StringContent(JsonConvert.SerializeObject(content), System.Text.Encoding.UTF8, "application/json");
return content

我试图实现的是将从我的另一个API以JSON形式返回的响应映射到TeamName列表中,TeamName POCO与API中的内容完全匹配。它看起来像这样:

public class TeamName
{
[JsonProperty("guid")]
public Guid Guid { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("priorityNumber")]
public int PriorityNumber { get; set; }
}

我对如何将内容作为TeamName 列表返回感到困惑

您可以使用Newtonsoft.Json.JsonConvert类中的DeserializeObject方法,如下所示:

var response = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<List<TeamName>>(response);

最新更新