Diserialize Json Always Get Items null

mkshixfv  于 2023-04-22  发布在  其他
关注(0)|答案(1)|浏览(133)

我正在反序列化一个JSON文件,这是我的Classes结构:

public class ControlInfoDataGroup
{
    public string UniqueId { get; private set; }
    public ObservableCollection<ControlInfoDataItem> Items { get; private set; }
    ...
} 

public class ControlInfoDataItem
{
    public string UniqueId { get; private set; }
    public ObservableCollection<ControlInfoDataItem> SubItems { get; private set; }
    ...
}

然后我用这些代码反序列化我的JSON:

public class JsonFileReader
{
    public ObservableCollection<ControlInfoDataGroup> Groups { get; set; }
        
    Groups = new ObservableCollection<ControlInfoDataGroup>();
    using FileStream openStream = File.OpenRead(JsonFilePath);
    var controlInfoDataGroup = await JsonSerializer.DeserializeAsync<JsonFileReader>(openStream, new JsonSerializerOptions
         {
                WriteIndented = true,
                PropertyNameCaseInsensitive = true
         });
}

这是我的json文件内容:

{
      "Groups": [
        {
          "Title": "Home",
          "IsSpecialSection": false,
          "HideGroup": false,
          "IsSingleGroup": false,
          "IsExpanded": false,
          "Items": [
            {
              "Title": "Movie",
              "IsNew": false,
              "IsUpdated": false,
              "IsPreview": false,
              "HideItem": false,
              "HideNavigationViewItem": false,
              "HideSourceCodeAndRelatedControls": false,
              "IncludedInBuild": false
            }
          ],
          "InfoBadge": null
        }
      ]
    }

我只得到“家”和这个项目没有任何“项目”,但根据我的json文件,我有一个项目的“家”称为“电影”.问题在哪里?我怎么能解决它?

owfi6suc

owfi6suc1#

你有奇怪的类,试试这些

Data controlInfoDataGroup =  await JsonSerializer.DeserializeAsync<Data>(openStream,new JsonSerializerOptions
    {
        WriteIndented = true,
        PropertyNameCaseInsensitive = true
    });

public class Data
{
    public ObservableCollection<ControlInfoDataGroup> Groups { get; set; }
}
public class ControlInfoDataGroup
{
    public string Title { get; set; }
    public bool IsSpecialSection { get; set; }
    public bool HideGroup { get; set; }
    public bool IsSingleGroup { get; set; }
    public bool IsExpanded { get; set; }
    public ObservableCollection<ControlInfoDataItem> Items { get; set; }
    public object InfoBadge { get; set; }
}

public class ControlInfoDataItem
{
    public string Title { get; set; }
    public bool IsNew { get; set; }
    public bool IsUpdated { get; set; }
    public bool IsPreview { get; set; }
    public bool HideItem { get; set; }
    public bool HideNavigationViewItem { get; set; }
    public bool HideSourceCodeAndRelatedControls { get; set; }
    public bool IncludedInBuild { get; set; }
}

相关问题