HttpClient来自官方的JSON扩展方法
2021-03-22 20:24
标签:client response like oca enc read 常见 rom status
Json的序列化和反序列化是我们日常常见的操作,通过System.Net.Http.Json我们可以用少量的代码实现上述操作.正如在github设计文档中所描述 他的依赖项也非常的少目前只依赖System.Net.Http, System.Text.Json 目前它还是预览版本 通过ReadFromJsonAsync直接可以反序列化 还可以以下面这种简洁方式使用 还可以通过NotSupportedException和JsonException异常类处理相应的异常. https://github.com/hueifeng/BlogSample/tree/master/src/SYSTEMNETHTTPJSON https://www.stevejgordon.co.uk/sending-and-receiving-json-using-httpclient-with-system-net-http-json https://github.com/dotnet/designs/blob/d4018c99c8134e9114a869e2e73a050059b9e663/accepted/2020/json-http-extensions/json-http-extentions.md HttpClient来自官方的JSON扩展方法 标签:client response like oca enc read 常见 rom status 原文地址:https://www.cnblogs.com/lonelyxmas/p/12695998.htmlSystem.Net.Http.Json
Serializing and deserializing JSON payloads from the network is a very
common operation for clients, especially in the upcoming Blazor
environment. Right now, sending a JSON payload to the server requires
multiple lines of code, which will be a major speed bump for those
customers. We‘d like to add extension methods on top of HttpClient that
allows doing those operations with a single method call.
System.Text.Json相对于Newtonsoftjson平均快了两倍,如果有兴趣相关基准测试可在这个文章中查阅
https://devblogs.microsoft.com/dotnet/try-the-new-system-text-json-apis/在.NET中安装和使用
dotnet add package System.Net.Http.Json
public static async Task
public static async Task
_client.GetFromJsonAsync
if (response.IsSuccessStatusCode)
{
try
{
return await response.Content.ReadFromJsonAsync
Reference
文章标题:HttpClient来自官方的JSON扩展方法
文章链接:http://soscw.com/index.php/essay/67656.html