反序列化 JSON 始终获取项目为 null

huangapple go评论64阅读模式
英文:

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&lt;ControlInfoDataItem&gt; Items { get; private set; }
    ...
} 

public class ControlInfoDataItem
{
    public string UniqueId { get; private set; }
    public ObservableCollection&lt;ControlInfoDataItem&gt; SubItems { get; private set; }
    ...
}

then i deserialize my json with this codes:

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

and this is my json file content:

    {
      &quot;Groups&quot;: [
        {
          &quot;Title&quot;: &quot;Home&quot;,
          &quot;IsSpecialSection&quot;: false,
          &quot;HideGroup&quot;: false,
          &quot;IsSingleGroup&quot;: false,
          &quot;IsExpanded&quot;: false,
          &quot;Items&quot;: [
            {
              &quot;Title&quot;: &quot;Movie&quot;,
              &quot;IsNew&quot;: false,
              &quot;IsUpdated&quot;: false,
              &quot;IsPreview&quot;: false,
              &quot;HideItem&quot;: false,
              &quot;HideNavigationViewItem&quot;: false,
              &quot;HideSourceCodeAndRelatedControls&quot;: false,
              &quot;IncludedInBuild&quot;: false
            }
          ],
          &quot;InfoBadge&quot;: 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?

反序列化 JSON 始终获取项目为 null

答案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&lt;Data&gt;(openStream,new JsonSerializerOptions
	{
		WriteIndented = true,
		PropertyNameCaseInsensitive = true
	});


public class Data
{
	public ObservableCollection&lt;ControlInfoDataGroup&gt; 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&lt;ControlInfoDataItem&gt; 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>



huangapple
  • 本文由 发表于 2023年4月17日 20:04:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76034977.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定