英文:
Deserialize Json Always Get Items null
问题
I am deserializing a Json file, this is my Classes structures:
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; }
...
}
then I deserialize my JSON with this code:
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
});
}
and this is my JSON file content:
{
"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
}
]
}
I only get "Home," and this item does not have any "Items," but according to my JSON file, I have an item for "Home" called "Movie."
Where is the issue? How can I fix it?
英文:
I am deserializing a Json file, this is my Classes structures:
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; }
...
}
then i deserialize my json with this codes:
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
});
}
and this is my json file content:
{
"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
}
]
}
i only get "Home" and this item does not have any "Items", but according my json file, i have an item for "Home" called "Movie".
where is issue? how i can fix it?
答案1
得分: 0
你有奇怪的类,尝试这些:
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; }
}
英文:
you have strange classes, try these
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; }
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论