默认情况下,序列化程序会转义所有非 ASCII 字符。 即,会将它们替换为 \uxxxx ,其中 xxxx 为字符的 Unicode 代码。

例如,如果以下 JSON 中的 sign 属性为中文还有符号等 " 中文 Holle Word abc 1+1<1>1&1'1$ ",则SingModel对象会进行序列化,如以下示例中所示:

//转Json之前
    ver = "1.0", 
    identifier = "admin", 
    appid = 10000,
    sign = "中文 Holle Word abc 1+1<1>1&1'1$" 
//System.Text.Json转Json之后
    ver = "1.0", 
    identifier = "admin", 
    appid = 10000,
    sign = "\u4E2D\u6587 Holle Word abc 1\u002B1\u003C1\u003E1\u00261\u00271$" 

解决办法:

一、序列化语言字符集

若要序列化一种或多种语言的字符集而不进行转义,请在创建 System.Text.Encodings.Web.JavaScriptEncoder 的实例时指定 Unicode 范围,如以下示例中所示:

using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Unicode;
options = new JsonSerializerOptions
    Encoder = JavaScriptEncoder.Create(UnicodeRanges.BasicLatin, UnicodeRanges.Cyrillic)
jsonString = JsonSerializer.Serialize(singModel, options);

此代码不转义西里尔文或希腊语字符。 如果 Summary 属性设置为西里尔文 жарко,则 WeatherForecast 对象会进行序列化,如以下示例中所示:

若要序列化所有语言集而不进行转义,请使用 UnicodeRanges.All
想了解其他的语言集,请移步:UnicodeRanges类 

二、序列化特定字符

一种替代方法是指定要允许的单个字符,而不进行转义。 下面的示例仅序列化 жарко 的前两个字符:

using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Unicode;
var encoderSettings = new TextEncoderSettings();
encoderSettings.AllowCharacters('\u0436', '\u0430');
encoderSettings.AllowRange(UnicodeRanges.BasicLatin);
options = new JsonSerializerOptions
    Encoder = JavaScriptEncoder.Create(encoderSettings),
    WriteIndented = true
jsonString = JsonSerializer.Serialize(weatherForecast, options);

三、序列化所有字符(建议使用这种方式)

若要最大程度地减少转义,可以使用 JavaScriptEncoder.UnsafeRelaxedJsonEscaping,如以下示例中所示:

using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Unicode;
options = new JsonSerializerOptions
    Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
    WriteIndented = true
jsonString = JsonSerializer.Serialize(weatherForecast, options);

与默认编码器相比,UnsafeRelaxedJsonEscaping 编码器在允许字符通过而不进行转义方面更加宽松:

  • 它不转义 HTML 敏感字符,如 <>& 和 '
  • 它不提供任何针对 XSS 或信息泄露攻击(如客户端和服务器在字符集方面不一致所可能导致的攻击)的额外深度防御保护。

仅当知道客户端将生成的有效负载解释为 UTF-8 编码的 JSON 时,才使用不安全编码器。 例如,如果服务器在发送响应标头 Content-Type: application/json; charset=utf-8,则可以使用它。 永远不允许将原始 UnsafeRelaxedJsonEscaping 输出发出到 HTML 页面或 <script> 元素。

1、如何使用 System.Text.Json 自定义字符编码 | Microsoft Docs

2、UnicodeRanges 类 (System.Text.Unicode) | Microsoft Docs

3、如何使用 System.Text.Json 自定义字符编码 | Microsoft Docs

4、JavaScriptEncoder.UnsafeRelaxedJsonEscaping 属性 | Microsoft Docs

本文主要介绍NET Core 3.0 JsonSerializer(System.Text.Json)中,使用Utf8JsonWriter和Utf8JsonReader生成及读取UFT-8编码JSON字符串的方法及示例代码。 原文地址:.NET Core 3.0 JsonSerializer中Utf8JsonWriter和Utf8JsonReader使用及示例 F#区分工会(包括结构工会),并具有多种表示形式; F#集合: list<'T> , Map<'T> , Set<'T> 。 它提供了许多自定义选项,允许使用多种JSON序列化格式。 FSharp.SystemTextJson是否支持联合的替代格式? FSharp.SystemTextJson是否支持将'T option表示为'T或null (或缺少字段)? 是的! 从v0.6开始,这是默认行为。 要取代它,使用一个明确的JsonUnionEncoding不包括UnwrapOption 。 FSharp.SystemTextJson是否在记录字段上支持JsonPropertyNameAttribute和JsonIgn
System.Text.Json.NET Core 3.0 新引入的高性能 json 解析、序列化、反序列化类库,武功高强,但毕竟初入江湖,炉火还没纯青,使用时需要注意,以下是我们在实现使用中遇到的一下问题。 1)默认设置下中文会被编码 序列化时中文会被编码,详见博问 .NET Core 3.0 中使用 System.Text.Json 序列化中文时的编码问题 。 解决方法: public void ConfigureServices(IServiceCollection services)
Newtonsoft.Json虽然使用广泛,功能强大,但在.NET Core中需要导入。 而System.Text.Json属于微软.NET Core中官方的包,相比于Newtonsoft.Json包来说,是一个精简的包。 尤其是Newtonsoft.Json作者加入微软之后,微软官方包的运行效率已经远远超越Newtonsoft.Json包。 因此,在开发中,尽可能来使用System.Text.Json,必经这玩意是微软的亲儿子。 在对一个JSON字符串进行格式化的时候,使用System.Text.Json
跨域问题Access to XMLHttpRequest‘*‘from origin ‘*‘ has been blocked by CORS..Access-Control-Allow-Origin 329103 跨域问题解决方案Access to XMLHttpRequest *from origin* has been blocked by CORS..Access-Control-Allow-Origin 20137