System.Text.Json.Serialization 似乎不适用于 JSON with NESTED 类
System.Text.Json.Serialization Does not appear to work for JSON with NESTED classes
如何定义仅使用 System.Text.Json.Serialization 的 class?
使用 Microsoft 的新替代 Newtonsoft 反序列化目前不适用于嵌套 classes,因为当反序列化 JSON 文件时所有属性都设置为 null。使用 Newtosonsoft 的 Json 属性 属性 [JsonProperty("Property1")]
维护属性值。
谢谢!
public class Class1
{
[JsonProperty("Property1")]
public string Property1 { get; set; }
}
正在使用 visual studio 的粘贴 JSON 到 class 以创建 class:
public class Rootobject
{
public string Database { get; set; }
public Configuration Configuration { get; set; }
}
public class Configuration
{
public Class1 Class1 { get; set; }
public Class2 Class2 { get; set; }
public Class3 Class3 { get; set; }
}
public class Class1
{
public string Property1 { get; set; }
}
public class Class2
{
public string Property2 { get; set; }
}
public class Class3
{
public string Property3 { get; set; }
}
问题是当 using System.Text.Json.Serialization 嵌套 class 的属性设置为 null 时。
{
"property1": null
}
csharp2json
使用 Newtonsoft 解串器与 [JsonProperty("Configuration")]
配合使用
public class Class1
{
[JsonProperty("Property1")]
public string Property1 { get; set; }
}
public class Class2
{
[JsonProperty("Property2")]
public string Property2 { get; set; }
}
public class Class3
{
[JsonProperty("Property3")]
public string Property3 { get; set; }
}
public class Configuration
{
[JsonProperty("Class1")]
public Class1 Class1 { get; set; }
[JsonProperty("Class2")]
public Class2 Class2 { get; set; }
[JsonProperty("Class3")]
public Class3 Class3 { get; set; }
}
public class RootObject
{
[JsonProperty("Database")]
public string Database { get; set; }
[JsonProperty("Configuration")]
public Configuration Configuration { get; set; }
}
您似乎将 Newtonsoft.Json
的 [JsonProperty]
属性与 System.Text.Json
混合使用。那不行。
System.Text.Json
中的等效属性是 [JsonPropertyName]
。
您还需要匹配大小写(对于 System.Text.Json
,这就是属性被评估为 null 的原因)。来自您引用的docs:
By default, property name matching is case-sensitive. You can specify case-insensitivity.
因此,如果您的 json 属性如下所示:
{
"property1": "abc"
}
那么,您的 class 应该如下所示:
public class Class1
{
[JsonPropertyName("property1")]
public string Property1 { get; set; }
}
请注意 JsonPropertyName
属性中的“property1”是小写的。
检查这个 online demo。
要指定不区分大小写,您可以在序列化程序选项中使用 PropertyNameCaseInsensitive
:
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
};
var data = JsonSerializer.Deserialize<Root>(json, options);
如何定义仅使用 System.Text.Json.Serialization 的 class?
使用 Microsoft 的新替代 Newtonsoft 反序列化目前不适用于嵌套 classes,因为当反序列化 JSON 文件时所有属性都设置为 null。使用 Newtosonsoft 的 Json 属性 属性 [JsonProperty("Property1")]
维护属性值。
谢谢!
public class Class1
{
[JsonProperty("Property1")]
public string Property1 { get; set; }
}
正在使用 visual studio 的粘贴 JSON 到 class 以创建 class:
public class Rootobject
{
public string Database { get; set; }
public Configuration Configuration { get; set; }
}
public class Configuration
{
public Class1 Class1 { get; set; }
public Class2 Class2 { get; set; }
public Class3 Class3 { get; set; }
}
public class Class1
{
public string Property1 { get; set; }
}
public class Class2
{
public string Property2 { get; set; }
}
public class Class3
{
public string Property3 { get; set; }
}
问题是当 using System.Text.Json.Serialization 嵌套 class 的属性设置为 null 时。
{
"property1": null
}
csharp2json
使用 Newtonsoft 解串器与 [JsonProperty("Configuration")]
public class Class1
{
[JsonProperty("Property1")]
public string Property1 { get; set; }
}
public class Class2
{
[JsonProperty("Property2")]
public string Property2 { get; set; }
}
public class Class3
{
[JsonProperty("Property3")]
public string Property3 { get; set; }
}
public class Configuration
{
[JsonProperty("Class1")]
public Class1 Class1 { get; set; }
[JsonProperty("Class2")]
public Class2 Class2 { get; set; }
[JsonProperty("Class3")]
public Class3 Class3 { get; set; }
}
public class RootObject
{
[JsonProperty("Database")]
public string Database { get; set; }
[JsonProperty("Configuration")]
public Configuration Configuration { get; set; }
}
您似乎将 Newtonsoft.Json
的 [JsonProperty]
属性与 System.Text.Json
混合使用。那不行。
System.Text.Json
中的等效属性是 [JsonPropertyName]
。
您还需要匹配大小写(对于 System.Text.Json
,这就是属性被评估为 null 的原因)。来自您引用的docs:
By default, property name matching is case-sensitive. You can specify case-insensitivity.
因此,如果您的 json 属性如下所示:
{
"property1": "abc"
}
那么,您的 class 应该如下所示:
public class Class1
{
[JsonPropertyName("property1")]
public string Property1 { get; set; }
}
请注意 JsonPropertyName
属性中的“property1”是小写的。
检查这个 online demo。
要指定不区分大小写,您可以在序列化程序选项中使用 PropertyNameCaseInsensitive
:
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
};
var data = JsonSerializer.Deserialize<Root>(json, options);