使用 System.Text.Json 使用 UNICODE 字符序列化期间出现问题
Problem during serialization with UNICODE char with System.Text.Json
我在反序列化期间修改数据的序列化过程中遇到问题(我认为这不是问题所在)
一些 \u 字符未正确转换...我不知道如何解决这个问题...即使使用全局编码器...
查看我对有问题和无问题的评论
using System;
using System.Text.Encodings.Web;
using System.Text.Unicode;
using System.Text.Json;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Json;
using System.Threading.Tasks;
using libaurore;
namespace partoo
{
class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("Hello World!");
var client = new HttpClient();
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Add("Accept", "application/json");
client.DefaultRequestHeaders.Add("pe-id-environnement", "pe-id-environnement");
client.DefaultRequestHeaders.Add("pe-id-correlation", "pe-id-correlation");
client.DefaultRequestHeaders.Add("pe-id-utilisateur", "pe-id-utilisateur");
client.DefaultRequestHeaders.Add("pe-nom-application", "pe-nom-application");
var CodesAurore = await client.GetFromJsonAsync<List<RefAURORE>>("http://xx010-.pole-emploi.intra/v1/agences?horaire=true");
Console.WriteLine(CodesAurore[500].NomAgence);
//display: Pôle emploi - L'hay-Les-Roses NO PROBLEM
var encoderSettings = new TextEncoderSettings();
//encoderSettings.AllowCharacters('\u0027'); dont fix....
encoderSettings.AllowRange(UnicodeRanges.All);
var options = new JsonSerializerOptions
{
Encoder = JavaScriptEncoder.Create(encoderSettings),
WriteIndented = false
};
var ff = JsonSerializer.Serialize(CodesAurore[500], options);
Console.WriteLine(ff);
//diplay:..,"libelle":"Pôle emploi - L\u0027hay-Les-Roses" PROBLEM!!!
}
}
}
class RefAURORE
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text.Json.Serialization;
namespace libaurore
{
public static class Datas
{
public static TextInfo myTI = new CultureInfo("fr-FR", false).TextInfo;
public static Dictionary<int, string> HJdico = new Dictionary<int, string> {
{0, "sunday"},{1, "monday"},{2, "tuesday"},{3, "wednesday"},{4, "thursday"},{5, "friday"},{6, "saturday"}};
}
public class RefAURORE
{
[DefaultValue("")]
[JsonPropertyName(name: "code")]
public string CodeAurore { get; set; }
[DefaultValue("")]
[JsonPropertyName(name: "type")]
public string TypeAgence {
get; set; }
private string _NomAgence;
[DefaultValue("")]
[JsonPropertyName(name: "libelle")]
public string NomAgence
{
get => _NomAgence;
set
{
_NomAgence = $"Pôle emploi - {Datas.myTI.ToTitleCase(value.ToLower())}";
if( CodeAurore == "IDF0291")
{
//display Pôle emploi - L'hay-Les-Roses.....NO PROBLEM
Console.WriteLine("***********");
Console.WriteLine(NomAgence);
Console.WriteLine("***********");
}
}
}
:
:
你能用下面的代码处理吗?
var response = "Željko Cvijetić";
var jsonSerializerSettings = new JsonSerializerSettings {
StringEscapeHandling = StringEscapeHandling.EscapeNonAscii
};
var json = JsonConvert.SerializeObject(response, jsonSerializerSettings);
并使用System.Text.Json
JsonSerializerOptions JsonSetting = new JsonSerializerOptions();
jso.Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping;
var s = JsonSerializer.Serialize(response, JsonSetting);
我在反序列化期间修改数据的序列化过程中遇到问题(我认为这不是问题所在)
一些 \u 字符未正确转换...我不知道如何解决这个问题...即使使用全局编码器...
查看我对有问题和无问题的评论
using System;
using System.Text.Encodings.Web;
using System.Text.Unicode;
using System.Text.Json;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Json;
using System.Threading.Tasks;
using libaurore;
namespace partoo
{
class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("Hello World!");
var client = new HttpClient();
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Add("Accept", "application/json");
client.DefaultRequestHeaders.Add("pe-id-environnement", "pe-id-environnement");
client.DefaultRequestHeaders.Add("pe-id-correlation", "pe-id-correlation");
client.DefaultRequestHeaders.Add("pe-id-utilisateur", "pe-id-utilisateur");
client.DefaultRequestHeaders.Add("pe-nom-application", "pe-nom-application");
var CodesAurore = await client.GetFromJsonAsync<List<RefAURORE>>("http://xx010-.pole-emploi.intra/v1/agences?horaire=true");
Console.WriteLine(CodesAurore[500].NomAgence);
//display: Pôle emploi - L'hay-Les-Roses NO PROBLEM
var encoderSettings = new TextEncoderSettings();
//encoderSettings.AllowCharacters('\u0027'); dont fix....
encoderSettings.AllowRange(UnicodeRanges.All);
var options = new JsonSerializerOptions
{
Encoder = JavaScriptEncoder.Create(encoderSettings),
WriteIndented = false
};
var ff = JsonSerializer.Serialize(CodesAurore[500], options);
Console.WriteLine(ff);
//diplay:..,"libelle":"Pôle emploi - L\u0027hay-Les-Roses" PROBLEM!!!
}
}
}
class RefAURORE
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text.Json.Serialization;
namespace libaurore
{
public static class Datas
{
public static TextInfo myTI = new CultureInfo("fr-FR", false).TextInfo;
public static Dictionary<int, string> HJdico = new Dictionary<int, string> {
{0, "sunday"},{1, "monday"},{2, "tuesday"},{3, "wednesday"},{4, "thursday"},{5, "friday"},{6, "saturday"}};
}
public class RefAURORE
{
[DefaultValue("")]
[JsonPropertyName(name: "code")]
public string CodeAurore { get; set; }
[DefaultValue("")]
[JsonPropertyName(name: "type")]
public string TypeAgence {
get; set; }
private string _NomAgence;
[DefaultValue("")]
[JsonPropertyName(name: "libelle")]
public string NomAgence
{
get => _NomAgence;
set
{
_NomAgence = $"Pôle emploi - {Datas.myTI.ToTitleCase(value.ToLower())}";
if( CodeAurore == "IDF0291")
{
//display Pôle emploi - L'hay-Les-Roses.....NO PROBLEM
Console.WriteLine("***********");
Console.WriteLine(NomAgence);
Console.WriteLine("***********");
}
}
}
:
:
你能用下面的代码处理吗?
var response = "Željko Cvijetić";
var jsonSerializerSettings = new JsonSerializerSettings {
StringEscapeHandling = StringEscapeHandling.EscapeNonAscii
};
var json = JsonConvert.SerializeObject(response, jsonSerializerSettings);
并使用System.Text.Json
JsonSerializerOptions JsonSetting = new JsonSerializerOptions();
jso.Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping;
var s = JsonSerializer.Serialize(response, JsonSetting);