在 json 中反序列化时,并非所有属性都来自文件

When Desrialize in json ,not all the properties come out from the file

当我试图反序列化到抽象 class 列表时,Book class 中的流派 属性 保持为 Null,而在 Journal class 中它从我的那里获取价值json 文件。

string folderPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
using (FileStream streamFile = File.Open($"{folderPath}//products10.json", FileMode.OpenOrCreate))
{
   using (StreamReader reader = new StreamReader(streamFile))
   {
       string fileContent = reader.ReadToEnd();
       productsList = JsonConvert.DeserializeObject<List<ProductBase>>(fileContent,ProductBase.StrandartJsonConvert);
   }
}

这是 JSON 文件:

[
    {
        "EditorName":"Me",
        "Name":"DailyMail",
        "IssueNumber":4,
        "Genres":[1],
        "Frequency":0,
        "Id":"01c26581-3e3a-4bc2-bc97-dfbab0215f29",
        "Description":"DailyMail",
        "PublicationDate":"2022-01-19T12:44:32.57574+02:00",
        "BasePrice":15.0,
        "Type":"Journal"
    },
    {
        "AuthorName":"Author",
        "Title":"HarryPotter",
        "Edition":3,
        "Geners":[2,1],
        "Synopsis":null,
        "Id":"6674b82d-6d6d-49ac-9c92-7d84b0dd09b6",
        "Description":"HarryPotter",
        "PublicationDate":"2022-01-19T12:44:30.2413124+02:00",
        "BasePrice":35.0,
        "Type":"Book"
    }
]

虽然在我的日记中 class 一切都得到了,但在我的书中 class - 它不是,看起来反序列化忽略了 Genres 属性。

public class Journal : ProductBase
{
    public string EditorName { get; set; }

    public string Name
    {
        get { return base.Description; }
        set { base.Description = value; }
    }

    public int IssueNumber { get; set; }

    public ICollection<JournalGenre> Genres { get; set; }

    public JournalFrequency Frequency { get; set; }

    public Journal(string editorName, string name, int issueNumber, DateTime publicationDate,
        decimal basePrice, JournalFrequency frequency, params JournalGenre[] genres)
        : base(name, publicationDate, basePrice)
    {
        this.EditorName = editorName;
        this.IssueNumber = issueNumber;
        this.Frequency = frequency;
        this.Genres = genres.ToList();
    }
}

这里所有属性都取值。

public class Book : ProductBase
{
    public string AuthorName { get; set; }
    public string Title 
    { 
        get { return base.Description; } 
        set { base.Description = value; } 
    }
    public int Edition { get; set; }
        
    public ICollection<BookGenre> Geners { get; set; }
        
    public string Synopsis { get; set; }

    public Book(string authorName, string title, DateTime publicationDate, decimal basePrice, int edition = 1, params BookGenre[] genres)
        :base(title, publicationDate, basePrice)
    {
        this.AuthorName = authorName;
        this.Edition = edition;
        this.Geners = genres.ToList();
    }
}

但这里的 Genres 保持为空 - const 中的 'genres' 不是从 JSON 文件中获取值 - 只有这个道具。其他任何东西都有价值。

不确定为什么@JamesS 删除了他的答案,但他是对的 - JSON 文件中的 属性 是 Geners 用于您的 Book,但构造函数参数是 genres.

将 class 和 JSON 文件中 属性 的拼写更正为 Genres:

public class Book : ProductBase
{
    ...    
    public ICollection<BookGenre> Genres { get; set; }
{
    "AuthorName":"Author",
    "Title":"HarryPotter",
    "Edition":3,
    "Genres":[2,1],
    "Synopsis":null,
    "Id":"6674b82d-6d6d-49ac-9c92-7d84b0dd09b6",
    "Description":"HarryPotter",
    "PublicationDate":"2022-01-19T12:44:30.2413124+02:00",
    "BasePrice":35.0,
    "Type":"Book"
}

或者更改构造函数参数的拼写以匹配 JSON 文件中使用的名称:

public Book(
    string authorName, 
    string title, 
    DateTime publicationDate, 
    decimal basePrice, 
    int edition = 1, 
    params BookGenre[] geners)