异常解析 json 与 System.Text.Json.Serialization
Exception parsing json with System.Text.Json.Serialization
我的示例代码很简单:
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);
最后一行加注:
Exception thrown: 'System.NullReferenceException' in System.Text.Json.dll
Object reference not set to an instance of an object.
我做错了什么?
(请注意这是最新的 .NET Core 3 预览版 5,最新 System.Text.Json 4.6.0-preview6.19259.10)
添加无参数构造函数可防止异常,但我没有 want/need 无参数构造函数,Json.Net 没有它也能正常解析。
有没有办法像 Json.Net 那样使用给定的构造函数使 System.Text.Json 解析?
PracticeName 需要是 属性,而不是字段。尝试使用无参数构造函数。
我开发了一个快速控制台程序class。 class C1
通过 Newtonsoft.Json 包转换。 class C2
通过 System.Text.Json 包解析。
using Newtonsoft.Json;
namespace TestJsonParse
{
class Program
{
static void Main(string[] args)
{
var c1 = new C1("1");
var json1 = JsonConvert.SerializeObject(c1); // returns "{\"PracticeName\":\"1\"}"
var x1 = JsonConvert.DeserializeObject<C1>(json1); // correctly builds a C1
var c2 = new C2();
string json2 = "{\"PracticeName\":\"1\"}";
var x2 = System.Text.Json.Serialization.JsonSerializer.Parse<C2>(json2); // correctly builds a C2
}
class C1
{
public C1(string PracticeName) { this.PracticeName = PracticeName; }
public string PracticeName;
}
class C2
{
public C2() { }
public string PracticeName { get; set; }
}
}
}
在当前状态下,JSON .NET Core 3.0 支持 is still not finished,似乎只支持无参数构造函数。将来可能会添加该功能。
当您想使用 .净框架。可能我们根本不应该为普通数据传输对象使用构造函数,因此我将其视为一种选择,而不是解决方法。
如果您搜索一种方法,关于如何从旧版本迁移到 .net core 3.0,或者无论如何使用 Newtonsoft.Json
,这已记录在案 here:
MVC:
安装 Microsoft.AspNetCore.Mvc.NewtonsoftJson
包,并将其注册到您的服务中:
services.AddMvc().AddNewtonsoftJson();
安装Microsoft.AspNetCore.SignalR.Protocols.NewtonsoftJson
包
//Client
new HubConnectionBuilder()
.WithUrl("/chatHub")
.AddNewtonsoftJsonProtocol(...)
.Build();
//Server
services.AddSignalR().AddNewtonsoftJsonProtocol(...);
这样你应该*能够使用 Json.NET .Net Core 3.0 中的功能
*我没有安装,无法测试
Json 对 System.Text.Json.Serialization 的支持非常不稳定。最好使用 NewtonSoft.Json 因为它更稳定并且几乎每个人都使用它。如果 运行 是一个控制台应用程序,那么从您终端的文件夹中执行
虽然您不需要指定版本。如果使用 visual studio 然后使用 Nuget 包管理器安装 Newtonsoft.json.
从文件中读取 json 例如 data.json
{
"Name":"Logan",
"Age": 30
"Books": ["Infinity War", "End Game"]
}
指定您的属性示例
class Person
{
public string Name{get; set;}
public int Age{get; set;}
public IList<string> Books{get; set;}
}
然后从 class 您希望访问 json 数据执行以下操作
string fileContent = System.IO.File.ReadAllText(@"./data.json");
Person person = Newtonsoft.Json.JsonConverter.DeserializeObject<Person>(fileContent);
测试一下
Console.WriteLine($"Hi {person.Name} you are {person.Age} today read {person.Books[0]}");
有关详细信息,请转到 https://www.newtonsoft.com/json/help/html/DeserializeObject.htm
我的示例代码很简单:
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);
最后一行加注:
Exception thrown: 'System.NullReferenceException' in System.Text.Json.dll Object reference not set to an instance of an object.
我做错了什么?
(请注意这是最新的 .NET Core 3 预览版 5,最新 System.Text.Json 4.6.0-preview6.19259.10)
添加无参数构造函数可防止异常,但我没有 want/need 无参数构造函数,Json.Net 没有它也能正常解析。
有没有办法像 Json.Net 那样使用给定的构造函数使 System.Text.Json 解析?
PracticeName 需要是 属性,而不是字段。尝试使用无参数构造函数。
我开发了一个快速控制台程序class。 class C1
通过 Newtonsoft.Json 包转换。 class C2
通过 System.Text.Json 包解析。
using Newtonsoft.Json;
namespace TestJsonParse
{
class Program
{
static void Main(string[] args)
{
var c1 = new C1("1");
var json1 = JsonConvert.SerializeObject(c1); // returns "{\"PracticeName\":\"1\"}"
var x1 = JsonConvert.DeserializeObject<C1>(json1); // correctly builds a C1
var c2 = new C2();
string json2 = "{\"PracticeName\":\"1\"}";
var x2 = System.Text.Json.Serialization.JsonSerializer.Parse<C2>(json2); // correctly builds a C2
}
class C1
{
public C1(string PracticeName) { this.PracticeName = PracticeName; }
public string PracticeName;
}
class C2
{
public C2() { }
public string PracticeName { get; set; }
}
}
}
在当前状态下,JSON .NET Core 3.0 支持 is still not finished,似乎只支持无参数构造函数。将来可能会添加该功能。
当您想使用 .净框架。可能我们根本不应该为普通数据传输对象使用构造函数,因此我将其视为一种选择,而不是解决方法。
如果您搜索一种方法,关于如何从旧版本迁移到 .net core 3.0,或者无论如何使用 Newtonsoft.Json
,这已记录在案 here:
MVC:
安装 Microsoft.AspNetCore.Mvc.NewtonsoftJson
包,并将其注册到您的服务中:
services.AddMvc().AddNewtonsoftJson();
安装Microsoft.AspNetCore.SignalR.Protocols.NewtonsoftJson
包
//Client
new HubConnectionBuilder()
.WithUrl("/chatHub")
.AddNewtonsoftJsonProtocol(...)
.Build();
//Server
services.AddSignalR().AddNewtonsoftJsonProtocol(...);
这样你应该*能够使用 Json.NET .Net Core 3.0 中的功能
*我没有安装,无法测试
Json 对 System.Text.Json.Serialization 的支持非常不稳定。最好使用 NewtonSoft.Json 因为它更稳定并且几乎每个人都使用它。如果 运行 是一个控制台应用程序,那么从您终端的文件夹中执行
虽然您不需要指定版本。如果使用 visual studio 然后使用 Nuget 包管理器安装 Newtonsoft.json.
从文件中读取 json 例如 data.json
{
"Name":"Logan",
"Age": 30
"Books": ["Infinity War", "End Game"]
}
指定您的属性示例
class Person
{
public string Name{get; set;}
public int Age{get; set;}
public IList<string> Books{get; set;}
}
然后从 class 您希望访问 json 数据执行以下操作
string fileContent = System.IO.File.ReadAllText(@"./data.json");
Person person = Newtonsoft.Json.JsonConverter.DeserializeObject<Person>(fileContent);
测试一下
Console.WriteLine($"Hi {person.Name} you are {person.Age} today read {person.Books[0]}");
有关详细信息,请转到 https://www.newtonsoft.com/json/help/html/DeserializeObject.htm