如何在 ASP.NET Core WebAPI 中使用 Newtonsoft: Json.NET?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43901618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I use Newtonsoft: Json.NET in ASP.NET Core WebAPI?
提问by DiPix
I found some information about nuget package - Newtonsoft: Json.NET
我找到了一些关于 nuget 包的信息 - Newtonsoft: Json.NET
As far I know it makes serialization from JSON to C# object much faster.
据我所知,它可以更快地从 JSON 序列化到 C# 对象。
My question: Is it enough to install this package to my ASP.NET CORE WebAPI project or I have bind it somehow, dunno maybe with middleware or something? And it will makes serialization faster?
我的问题:将这个包安装到我的 ASP.NET CORE WebAPI 项目是否足够,或者我以某种方式绑定了它,不知道可能是中间件还是什么?它会使序列化速度更快吗?
采纳答案by stybl
You don't need to do anything special to get Newtonsoft.JSON to work. Simply install it through NuGet (or manually) and you're good to go. You will however have to go through your code and replace any code you wrote to work with JSON before installing it.
您无需执行任何特殊操作即可让 Newtonsoft.JSON 正常工作。只需通过 NuGet(或手动)安装它就可以了。但是,在安装之前,您必须仔细检查您的代码并替换您为使用 JSON 而编写的任何代码。
EDIT:You dont even have to do that! Turns out .Net Core uses it out of the box. Refer to thisanswer for how to use it natively.
编辑:你甚至不必这样做!事实证明 .Net Core 开箱即用。有关如何在本机使用它,请参阅此答案。
UPDATE: From the answer linked above:
更新:从上面链接的答案:
This is only true for ASP.NET Core 1.0 to 2.2. ASP.NET Core 3.0 removes the dependency on JSON.NET and uses it's own JSON serializer.
这仅适用于 ASP.NET Core 1.0 到 2.2。ASP.NET Core 3.0 移除了对 JSON.NET 的依赖,并使用它自己的 JSON 序列化程序。
As for performance, there are a few benchmarks out there that compare alternative ways of serializing and deserializing JSON. A quick google search led me to this, as well as this.
回答by Salman Farsi
I think you can use it without adding any middleware. Follow how to use it on http://www.newtonsoft.com/json
我认为您可以在不添加任何中间件的情况下使用它。按照如何在http://www.newtonsoft.com/json上使用它

