[英]JSON serialization/deserialization in ASP.Net Core
Since there is no JavaScriptSerializer
, what native implementation can be used to handle this?由于没有JavaScriptSerializer
,可以使用什么本机实现来处理这个问题?
I noticed JsonResult
and I can format data to JSON with this, but how do I deserialize?我注意到JsonResult
并且我可以用JsonResult
数据格式化为 JSON,但是我如何反序列化?
Or maybe I am missing some dependencies in project.json
?或者我在project.json
缺少一些依赖项?
You can use Newtonsoft.Json
, it's a dependency of Microsoft.AspNet.Mvc.ModelBinding
which is a dependency of Microsoft.AspNet.Mvc
.您可以使用Newtonsoft.Json
,它是依赖Microsoft.AspNet.Mvc.ModelBinding
这是一个依赖Microsoft.AspNet.Mvc
。 So, you don't need to add a dependency in your project.json.因此,您无需在 project.json 中添加依赖项。
#using Newtonsoft.Json
....
JsonConvert.DeserializeObject(json);
Note, using a WebAPI controller you don't need to deal with JSON.请注意,使用 WebAPI 控制器不需要处理 JSON。
Json.NET has been removed from the ASP.NET Core 3.0 shared framework. Json.NET已从 ASP.NET Core 3.0 共享框架中删除。
You can use the new JSON serializer layers on top of the high-performance Utf8JsonReader
and Utf8JsonWriter
.您可以在高性能Utf8JsonReader
和Utf8JsonWriter
之上使用新的 JSON 序列化器层。 It deserializes objects from JSON and serializes objects to JSON.它从 JSON 反序列化对象并将对象序列化为 JSON。 Memory allocations are kept minimal and includes support for reading and writing JSON with Stream asynchronously.内存分配保持最小,并支持异步读取和写入 JSON 与 Stream。
To get started, use the JsonSerializer
class in the System.Text.Json.Serialization
namespace.首先,使用System.Text.Json.Serialization
命名空间中的JsonSerializer
类。 See the documentation for information and samples.有关信息和示例,请参阅文档。
To use Json.NET in an ASP.NET Core 3.0 project:在 ASP.NET Core 3.0 项目中使用 Json.NET:
services.AddMvc()
.AddNewtonsoftJson();
Read Json.NET support in Migrate from ASP.NET Core 2.2 to 3.0 Preview 2 for more information.阅读从 ASP.NET Core 2.2 迁移到 3.0 预览版 2 中的Json.NET 支持以了解更多信息。
.net core .net核心
using System.Text.Json;
var jsonStr = JsonSerializer.Serialize(MyObject)
var weatherForecast = JsonSerializer.Deserialize<MyObject>(jsonStr);
For more information about excluding properties and nulls check out This Microsoft side有关排除属性和空值的更多信息,请查看Microsoft 端
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.