Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将JSON对象反序列化为.NET哈希集和列表失败_C#_Arrays_Json_Asp.net Core_.net Core - Fatal编程技术网

C# 将JSON对象反序列化为.NET哈希集和列表失败

C# 将JSON对象反序列化为.NET哈希集和列表失败,c#,arrays,json,asp.net-core,.net-core,C#,Arrays,Json,Asp.net Core,.net Core,我有这样的Json我想保存: "data": { "importMannerTypeList": { "importMannerTypeID": 5, "importMannerTypeDesc": "a" }, "authoritySourceList": [ { &qu

我有这样的Json我想保存:

 "data": {
      "importMannerTypeList": {
        "importMannerTypeID": 5,
        "importMannerTypeDesc": "a"
      },
      "authoritySourceList": [
        {
          "authoritySourceID": 1,
          "authoritySourceDesc": "b"
        }
      ],
      "permitStatusList": {
        "permitStatusID": 4,
        "permitStatusDesc": "c"
      }}
它们都设置为数组,但因为
authoritySourceList
是多选的,而不是一个对象,所以看起来像这样。下面是反序列化json的类,
importMannertTypeList
permitStatusList
未能从json获取数据为什么

public class ImportPlantSettingsResponse
    {
        public ImportPlantSettingsResponse()
        {
            ImportMannerTypeList = new List<ImportMannerType>();
            AuthoritySourceList = new HashSet<AuthoritySource>();
            PermitStatusList = new List<PermitStatusResponse>();
        }

        public virtual List<ImportMannerType> ImportMannerTypeList { get; set; }
        public virtual ICollection<AuthoritySource> AuthoritySourceList { get; set; }
        public virtual List<PermitStatusResponse> PermitStatusList { get; set; }
公共类导入设备设置响应
{
公共进口设备设置响应()
{
ImportMannerTypeList=新列表();
AuthoritySourceList=新的HashSet();
PermitStatusList=新列表();
}
公共虚拟列表ImportMannerTypeList{get;set;}
公共虚拟ICollection AuthoritySourceList{get;set;}
公共虚拟列表PermitStatusList{get;set;}

正如Selim Yildiz所说,ImportMannerTypeList和PermitStatusList不是列表。下面是一个演示:

型号:

public class ImportPlantSettingsResponse
    {
        public ImportPlantSettingsResponse()
        {
            AuthoritySourceList = new HashSet<AuthoritySource>();
        }

        public virtual ImportMannerType ImportMannerTypeList { get; set; }
        public virtual ICollection<AuthoritySource> AuthoritySourceList { get; set; }
        public virtual PermitStatusResponse PermitStatusList { get; set; }
    }
结果:

我建议您阅读,因为
importMannertTypeList
的值在Json中不是数组,
permitStatusList
也是。这些不是Json数组,它们是Json对象。您问题中显示的Json没有任何数组,这些数组是以
[
开头,以
结尾的容器
,并包含逗号分隔的JSON值。有关详细信息,请参阅。由于Newtonsoft根据将集合(如
HashSet
)序列化为JSON数组,因此如果没有自定义转换器,您将无法将非数组对象反序列化为哈希集。
public IActionResult TestImportPlantSettingsResponse([FromBody] ImportPlantSettingsResponse importPlantSettingsResponse) {
            return Ok();
        }