且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从ASP.NET Core Web API返回JSON关联数组的问题

更新时间:2023-10-26 09:04:04

此答案来自@dbc.我没有意识到System.Text.Json很难对Dictionary进行序列化,而通常与Dictionary无关,而对于使用非字符串键的Dicitonary来说尤其如此.对于我们来说,编写一个转换器以将必要的Dictionary转换为字符串类型是很简单的,然后内置的JSON序列化就可以很好地处理它.

This answer comes from @dbc in the comments. I hadn't realized that the System.Text.Json difficulty with serializing Dictionary wasn't with Dictionary generally, but specifically with Dicitonary using a non-string key. It was trivial for us to write a converter to translate the necessary Dictionary to a string-keyed type, and then the built-in JSON serialization handled it just fine.

谢谢@dbc!