當我試圖反序列化為抽象類串列時,Book 類中的 Genres 屬性保持為 Null,而在 Journal 類中它從我的 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"
}
]
雖然在我的日記課上一切都進入了,但在我的書課上 - 它不是,看起來反序列化忽略了該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();
}
}
但是這里的流派保持為空 - const 中的“流派”沒有從 JSON 檔案中獲取值 - 只有這個道具。其他任何東西都會有價值。
uj5u.com熱心網友回復:
不知道為什么@JamesS 洗掉了他的答案,但他是對的——JSON 檔案中的屬性是Geners你的Book,但建構式引數是genres.
將類和 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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/415677.html
標籤:
上一篇:異步超過同步的可能方法
下一篇:IL切換指令
