试用新的System.Text.Json API

时间:2021-01-07 12:30:26   收藏:0   阅读:0

https://devblogs.microsoft.com/dotnet/try-the-new-system-text-json-apis/

对于所有示例,请确保导入以下两个名称空间:

using System.Text.Json;
using System.Text.Json.Serialization;
范例
class WeatherForecast
{
    public DateTimeOffset Date { get; set; }
    public int TemperatureC { get; set; }
    public string Summary { get; set; }
}

string Serialize(WeatherForecast value)
{
    return JsonSerializer.ToString<WeatherForecast>(value);
}

 反序列化

WeatherForecast Deserialize(string json)
{
    var options = new JsonSerializerOptions
    {
        AllowTrailingCommas = true
    };

    return JsonSerializer.Parse<WeatherForecast>(json, options);
}

 

 
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!