【发布时间】:2019-12-11 14:21:53
【问题描述】:
我是 .NET Core 的新手,正在尝试解决问题;练习如何使用 Flurl 使用 API。 使用返回 JSON 数组的端点 https://jsonplaceholder.typicode.com/posts,我尝试了以下代码,但列表包含我无法转换的动态。谁能建议如何将动态转换为适当的帖子对象?
public class PostsApiClient
{
public async Task<IEnumerable<PostInput>> GetPosts()
{
var response = await "https://jsonplaceholder.typicode.com/posts".GetJsonListAsync();
IEnumerable<Post> listOfPosts = response.Select(post => new Post
{
Title = post.Title,
Body = post.Body
});
return listOfPosts;
}
public class Post
{
public int UserId { get; set; }
public int Id { get; set; }
public string Title { get; set; }
public string Body { get; set; }
}
}
【问题讨论】:
标签: dynamic .net-core casting flurl