简体   繁体   English

ASP.Net Core 中的 JSON 序列化/反序列化

[英]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。

UPDATE ASP.Net Core 3.0更新 ASP.Net 核心 3.0

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 .您可以在高性能Utf8JsonReaderUtf8JsonWriter之上使用新的 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;

To serialize序列化

var jsonStr = JsonSerializer.Serialize(MyObject)

Deserialize反序列化

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.

相关问题 JSON 自定义序列化和反序列化 C# asp.net 内核中的现有类 - JSON custom serialization and deserialization of existing classes in C# asp.net core 迁移到 ASP.Net Core MVC 时 JSON 序列化/反序列化不起作用 - JSON serialization/deserialization not working when migrating to ASP.Net Core MVC 更改单个 ASP.NET 内核 controller 的 JSON 反序列化/序列化策略 - Change the JSON deserialization/serialization policy for single ASP.NET Core controller 忽略ASP.NET Core 2中XML序列化/反序列化的属性 - Ignore property from XML serialization/deserialization in ASP.NET Core 2 ASP.NET 内核处理 JSON 反序列化问题 - ASP.NET Core handling JSON deserialization problems ASP.NET Core 中每个控制器的不同 JSON 反序列化设置 - Different JSON deserialization settings per controller in ASP.NET Core ASP.NET JSON反序列化 - Asp.net json deserialization 如何在ASP.NET OWIN API项目中启用JSON.NET序列化/反序列化跟踪? - How to enable JSON.NET serialization/deserialization tracing in asp.net owin api project? 更改单个 ASP.NET Core 控制器的 JSON 序列化设置 - Change the JSON serialization settings of a single ASP.NET Core controller 在 asp.net webapi POST 中使用序列化或反序列化期间出错 - Error during serialization or deserialization using in asp.net webapi POST
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM