下面的代码产生以下输出:
{"x": "Art. 120 - Incapacità di intendere o di volere"}
我需要对此进行更改,我想我必须更改编码上的某些内容,但我不知道是什么:
{"x": "Art. 120 - Incapacit\u00e0 di intendere o di volere"}
code:
string label = "Art. 120 - Incapacità di intendere o di volere";
JObject j = new JObject();
j.Add(new JProperty("x", label));
string s = j.ToString();
Encoding encoding = new UTF8Encoding(false);
string filename = @"c:\temp\test.json";
using (FileStream oFileStream = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write))
{
using (StreamWriter oStreamWriter = new StreamWriter(oFileStream, encoding))
{
oStreamWriter.Write(j.ToString());
oStreamWriter.Flush();
oStreamWriter.Close();
}
oFileStream.Close();
}
就像其他人所说,你想使用 StringEscapeHandling.EscapeNonAscii
using System;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public class Program
{
public static void Main()
{
string label = "Art. 120 - Incapacità di intendere o di volere";
JObject j = new JObject();
j.Add(new JProperty("x", label));
string s = j.ToString();
var sr = new StringWriter();
var jsonWriter = new JsonTextWriter(sr) {
StringEscapeHandling = StringEscapeHandling.EscapeNonAscii
};
new JsonSerializer().Serialize(jsonWriter, j);
Console.Out.WriteLine(sr.ToString());
}
}
outputs
{"x":"Art. 120 - Incapacit\u00e0 di intendere o di volere"}
https://dotnetfiddle.net/s5VppR
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)