【发布时间】:2021-03-02 18:23:54
【问题描述】:
我正在开发一个 ASP.NET Core 3.1 网站,我在 Dictionary
我使用以下代码对其进行序列化。请注意,我已尝试过 Microsoft System.Text.Json 和 Newtonsoft。
jsonResult = Newtonsoft.Json.JsonConvert.SerializeObject(result);
//jsonResult = JsonSerializer.Serialize(result);
Dictionary中的数据应该按照如下类结构进行反序列化:
我使用以下代码:
//IEnumerable<SeccGralContenidoViewModel> seccGralContenido = JsonSerializer.Deserialize<IEnumerable<SeccGralContenidoViewModel>>(_seccGralContenidoRepository.Read());
IEnumerable<SeccGralContenidoViewModel> seccGralContenido = Newtonsoft.Json.JsonConvert.DeserializeObject <IEnumerable<SeccGralContenidoViewModel>>(_seccGralContenidoRepository.Read());
但是,无论我使用 Newtonsoft 还是 System.Text.Json,反序列化时都会引发异常:
我最初使用 System.Text.Json 命名空间,但我也尝试使用 Newtonsoft。在深入分析之后,我发现问题可能是数据保存到 Dictionary 的方式,但我没有找到解决方法。
【问题讨论】:
-
我是否理解正确,您希望将
" 1, 2, 6"反序列化为string[] { "1", "2", "6" },反之亦然。 -
您可以尝试将字符串 arrry 组装为
{CategoriasContenidolds, {1,2,3}}。 -
是的,我想将 "1, 2, 6" 反序列化为 string[] { "1", "2", "6" }
-
我已经尝试使用 PostgreSQL 函数 STRING_TO_ARRAY 将结果设为 {[CategoriasContenidoIds, {" 1"," 2"," 6"}]},但我仍然遇到同样的错误。
-
能否请您 edit 将您的代码和 JSON 包含为 文本 而不是屏幕截图?此处要求不要为此使用图像,请参阅Discourage screenshots of code and/or errors 和Why not upload images of code on SO when asking a question 了解原因。一个可编译、可运行的 minimal reproducible example 会很棒,并且可以最大限度地提高有人提供帮助的机会。
标签: json asp.net-core serialization deserialization