问题标签 [system.text.json]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
5 回答
9324 浏览

c# - System.Text.JSON 不会反序列化 Newtonsoft 所做的事情

我有一个 json,新System.Text.Json.JsonSerializer.Deserialize<T>(json_data)序列化List<T>的元素数量正确,但是里面的对象的所有值都为 null 或 0

Newtonsoft.Json.JsonConvert.DeserializeObject<T>(json_data)正确填充相同的 json 。

班级:

JSON

有什么帮助吗?consoleapp 项目 .net 核心 3.0.0 newtonsoft 12.0.3

在此处输入图像描述

0 投票
2 回答
29890 浏览

c# - JSON 值无法转换为 System.Nullable[System.Int32]

我将 ASP.NET Core 2.2 API 更新为 ASP.NET Core 3.0,并且我正在使用 System.Json:

然后我尝试使用以前工作的 Angular 8 发布 JSON 数据:

ASP.NET Core 3.0 API 中的模型是:

API控制器动作如下:

当我提交模型时,我收到以下错误:

使用 System.Json 而不是 Newtonsoft 时我需要做其他事情吗?

0 投票
2 回答
656 浏览

c# - 使用 System.Text.Json(.netcore-3.0) 而不是 Newtonsoft.Json 的扩展功能是什么?

我使用 Newtonsoft.Json 将 JSON 对象转换为字符串,反之亦然。System.Text.Json最近我在https://docs.microsoft.com/en-gb/dotnet/api/system.text.json?view=netcore-3.0中读到了。

我读了这篇文章,它比Newtonsoft.Json快。

使用 Newtonsoft.Json;

使用 System.Text;

0 投票
1 回答
1899 浏览

c# - System.Text.Json API 有类似 IContractResolver 的东西

在新的 System.Text.Json 中;命名空间是否有类似 IContractResolver 的东西,我正在尝试将我的项目从 Newtonsoft 迁移出来。

这是我要移动的课程之一:

0 投票
1 回答
630 浏览

asp.net-web-api2 - Web api 中的 System.Text.Json JsonRequired 替换

我有一个很好的谷歌,但似乎无法在 System.Text.Json(特别是 JsonRequired)中找到 Json.Net 注释的替代品。

新的 Microsoft 框架真的不是 Newtonsoft 的替代品吗?

0 投票
1 回答
255 浏览

c# - 如何使用嵌套的 JsonConverters?

我正在使用新System.Text.Json库尝试将一些 json 直接反序列化为不可变类。

默认情况下,无法构造对象,System.Text.Json因为它们没有空/默认构造函数,也没有任何公共设置器。

于是我开始构建 customJsonDecoder<T>来告诉库如何反序列化。

但是在这里我被卡住了。在反序列化作者的书籍结束时。
我如何重用现有的JsonDecoder<Book>来反序列化该数组?
谢谢您的帮助!:)

0 投票
1 回答
227 浏览

c# - 将 JSON 反序列化为 DateTime 属性会引发异常

我正在尝试反序列化以下 JSON:

通过此方法调用:

方法:

不幸的是,我收到以下错误:

System.Text.Json.JsonException: '无法将 JSON 值转换为 System.DateTime。路径:$.PublikationsDatum | 行号:0 | BytePositionInLine:353。

JSON 来自对此 API 方法的调用:

publikationsDatum是一个正常的DateTime属性


我究竟做错了什么?如何publikationsDatum将 JSON 反序列化回 DateTime?

提前致谢

编辑:我们没有使用任何 JSON 库,并希望保持这种状态。我们正在使用System.Text.Json

0 投票
1 回答
10353 浏览

c# - Modifying a JSON file using System.Text.Json

I know you can do this easily with Newtonsoft. As I am working with .NET Core 3.0, however, I am trying to use the new methods for interacting with JSON files —i.e., System.Text.Json—and I refuse to believe that what I am trying to do is all that difficult!

My application needs to list users that have not already been added to my database. In order to get the full list of all users, the app retrieves a JSON string from a web API. I now need to cycle through each of these users and check if they have already been added to my application before returning a new JSON list to my view so that it can display the new potential users to the end user.

As I am ultimately returning another JSON at the end of the process, I don't especially want to bother deserializing it to a model. Note that the structure of data from the API could change, but it will always have a key from which I can compare to my database records.

My code currently looks like this:

This seems like a lot of hoops to jump through in order to achieve something that would be fairly trivial with Newtonsoft.

Can anyone advise me on a better way of doing this—and, ideally, without the need for the UserObject?

0 投票
1 回答
1475 浏览

c# - 测试自定义 JsonConverter 时出现异常

我们以一种奇怪的格式从 API 中获取序列化的 DateTimes:/Date(1574487012797)/ 为了使用 反序列化这个值System.Text.Json,我们编写了自己的JsonConverter

我想为这个转换器写一个单元测试。我尝试的是以下内容:

不幸的是,在尝试执行单元测试时,我得到了一个InvalidOperationExceptionatvar dateTimeString = reader.GetString();

System.InvalidOperationException:'无法将令牌类型'None'的值作为字符串获取。'

如何正确设置测试/我做错了什么?

0 投票
3 回答
26930 浏览

c# - System.Text.Json:如何为枚举值指定自定义名称?

使用 .NET Core 中的System.Text.Json序列化程序功能,如何为枚举值指定自定义值,类似于JsonPropertyName?例如: