相关疑难解决方法(0)

使用System.Text.Json.Serialization解析json的异常

我的示例代码非常简单:

using System.Text.Json.Serialization;
using Newtonsoft.Json;

public class C {
  public C(string PracticeName) { this.PracticeName = PracticeName; }
  public string PracticeName;
}

var x = new C("1");
var json = JsonConvert.SerializeObject(x); // returns "{\"PracticeName\":\"1\"}"

var x1 = JsonConvert.DeserializeObject<C>(json); // correctly builds a C

var x2 = System.Text.Json.Serialization.JsonSerializer.Parse<C>(json);
Run Code Online (Sandbox Code Playgroud)

最后一行引发:

引发异常:System.Text.Json.dll中的“ System.NullReferenceException”对象引用未设置为对象的实例。

我究竟做错了什么 ?

(Note this is on latest .NET Core 3 preview 5 with latest System.Text.Json 4.6.0-preview6.19259.10)

Adding a parameterless constructor prevents the exception however I don't want/need a parameterless constructor and …

c# json.net .net-core .net-core-3.0 system.text.json

2
推荐指数
1
解决办法
3367
查看次数