Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将newtonsoft.json代码更改为system.text.json_C#_Json_System.text.json - Fatal编程技术网

C# 如何将newtonsoft.json代码更改为system.text.json

C# 如何将newtonsoft.json代码更改为system.text.json,c#,json,system.text.json,C#,Json,System.text.json,我想完全迁移到.NET Core,所以我需要使用System.Text.Json而不是Newtonsoft.Json 如何在System.Text.Json中编写此代码 private readonly JsonSerializer _serializer; _serializer = JsonSerializer.Create(new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.IgnoreAn

我想完全迁移到.NET Core,所以我需要使用System.Text.Json而不是Newtonsoft.Json

如何在System.Text.Json中编写此代码

private readonly JsonSerializer _serializer;

_serializer = JsonSerializer.Create(new JsonSerializerSettings
{
  DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate
});

private JObject _jsonSettings;

protected override void LoadSection(string sectionName, object section)
{    
  var jsonSection = _jsonSettings[sectionName];
  if (jsonSection != null)
  {
    using (var reader = jsonSection.CreateReader())
    {
      _serializer.Populate(reader, section);
    }
  }
}

protected override void SaveSection(string sectionName, object section)
{        
  var settings = _jsonSettings ?? new JObject();
  settings[sectionName] = JObject.FromObject(section);
  _jsonSettings = settings;
}

protected override void LoadDefaults()
{
  _jsonSettings = new JObject();
}

private void LoadFromJson(string json)
{
  _jsonSettings = JObject.Parse(json);
}

请向官员咨询

提供了3.1和5个版本。请注意,在3.1中,您可以安装5.0包以获得新功能(例如反序列化字段)