来自空字典的 JsonSerializationException json

JsonSerializationException from empty dictionary json

我正在尝试将设置为 "{}" 的 json 字符串作为空字典进行除菌处理,但我看到了这个错误:

Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.String,System.String]' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path '', line 1, position 1.
        public Dictionary<string, string> FileMetadataItems { get; set; } = new Dictionary<string, string>();

这是我的代码:

        private IQueryable<FileSystemItemMetadataDTO> GetFileSystemMetadataQueryable()
        {
            var files = BIContext.FileSystemItems
                        .Select(x => new FileSystemItemMetadataDTO()
                        {
                            FileId = x.FileId,
                            FileName = x.FileName,
                            FileType = x.FileType,
                            IsFolder = x.IsFolder,
                            LastWriteTime = x.LastWriteTime,
                            ParentId = x.ParentId,
                            FileLength = 0,
                            FileSystemItemDataId = x.FileSystemItemDataId,
                            FileSize = x.FileSize,
                            FileMetadata = x.FileMetadata,
                            ModuleId = x.ModuleId,
                            FileMetadataItems = JsonConvert.DeserializeObject<Dictionary<string, string>>(x.FileMetadata),
                        });

            return files;
        }

关于错误发生原因的任何想法?

我试过如下设置一个空字典,但也出错了。

{"name":"value"}) 

我也试过这个:

        private IQueryable<FileSystemItemMetadataDTO> GetFileSystemMetadataQueryable()
        {
            var files = BIContext.FileSystemItems
                        .Select(x => new FileSystemItemMetadataDTO()
                        {
                            FileId = x.FileId,
                            FileName = x.FileName,
                            FileType = x.FileType,
                            IsFolder = x.IsFolder,
                            LastWriteTime = x.LastWriteTime,
                            ParentId = x.ParentId,
                            FileLength = 0,
                            FileSystemItemDataId = x.FileSystemItemDataId,
                            FileSize = x.FileSize,
                            FileMetadata = x.FileMetadata,
                            ModuleId = x.ModuleId,
                            FileMetadataItems = x.FileMetadata == "{}" ? new Dictionary<string, string>() : JsonConvert.DeserializeObject<Dictionary<string, string>>(x.FileMetadata),
                        });

            return files;
        }

问题似乎是由于使用 IQueryable<FileSystemItemMetadataDTO> GetFileSystemMetadataQueryable() 方法执行 JsonConvert.DeserializeObject<Dictionary<string, string>>(x.FileMetadata) 造成的。一旦我移动到我的服务层,它工作正常!