JsonConvert.SerializeObject 读取结构,但不读取值

JsonConvert.SerializeObject reads the structure, but not the values

我试过这段源代码(基于 NewtonSoft 的 JSON NuGet 库),用于将 JSON 文件读入 JSON 对象:

string str_File_Content = File.ReadAllText(openFileDialog1.FileName);
Rootobject existing_root = JsonConvert.DeserializeObject<Rootobject>(str_File_Content);

...它几乎成功了:所有 JSON 个对象都被加载到 existing_root 中,并且在数组的情况下,对象的数量似乎是正确的。

但是:属性的值好像没有填写,可以看到这里:

JSON 文件摘录:

{
    "project": {
        "common.DESCRIPTION": "Some information",

existing_root 手表摘录 window:

Expected    :existing_root.project.commonDESCRIPTION : Some information
Real result :existing_root.project.commonDESCRIPTION : null

我该怎么做才能使 JsonConvert.DeserializeObject() 不仅处理结构,还处理值?

您的 json 属性 名称包含“.”符号,对于 C# 属性 名称无效,因此您可以指定正确的名称以在(反)序列化期间使用 JsonPropertyAttribute:

public class Project
{
    [JsonProperty("common.DESCRIPTION")]
    public string commonDESCRIPTION { get; set; }
}