简体   繁体   English

jsonSerializer.Deserialize对于C#中的Unicode字符无法正常工作

[英]jsonSerializer.Deserialize not working properly for unicode characters in c#

I want to deserialise a JSON object containing unicode data to string array. 我想反序列化包含Unicode数据到字符串数组的JSON对象。 While the characters inside the JSON object is english, it works fine. 尽管JSON对象中的字符是英语,但效果很好。 But when I use chinese it won't. 但是当我使用中文时不会。

JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
string[] SampleText = jsonSerializer.Deserialize<string[]>(HttpContext.GetGlobalResourceObject("Resource", "SampleText").ToString());

When I run this in Immediate window of visual studio, the out comes like this for English 当我在Visual Studio的“即时”窗口中运行此命令时,输出像这样的英语

jsonSerializer.Deserialize<string[]>(HttpContext.GetGlobalResourceObject("Resource", "SampleText").ToString());
{string[3]}        
    [0]: "Size"
    [1]: "Name"
    [2]: "Type"

But for chinese an exception occurs 但是中国人例外

base {System.SystemException}: {"Invalid JSON primitive: ."}
Message: "Invalid JSON primitive: ."
ParamName: null

The resourse file value for english and chinese 英文和中文的资源文件值

["Size","Name","Type"]
[“大小”,“姓名”,“類型”]

EDIT: I just noticed that the japanese text is surrounded by smart quotes “ ” instead of actual quotes " . Replace the smart quotes with simple quotes. No language that I know of uses smart quotes as text delimiters, they are treated as content. Your text also uses a non-comma character (hex ff0c) instead of a comma. 编辑:我只注意到了日本文由智能引号包围“ ” ,而不是实际的报价" 。简单的引号替换智能引号没有语言,我知道用途的智能引号作为文本分隔符,它们被视为内容你。文本还使用非逗号字符 (十六进制ff0c)代替逗号。

Strings in .NET are Unicode. .NET中的字符串是Unicode。 It isn't possible to create a non -Unicode string. 无法创建 -Unicode字符串。

Problems occur only when you try to read non-Unicode content (like a file encoded in a specific codepage) as if it were a Unicode file. 仅当您尝试读取非Unicode内容(例如在特定代码页中编码的文件)时,才会出现问题,就好像它是Unicode文件一样。 The OS (and .NET) will use the system locale to read non-unicode files which can result in garbled data. 操作系统(和.NET)将使用系统区域设置来读取非Unicode文件,这会导致数据乱码。 The solution is to either save files with Unicode encoding, or explicitly specify the file's encoding if it's different than the system locale. 解决方案是使用Unicode编码保存文件,或者如果文件的编码与系统区域设置不同,则明确指定文件的编码。

In your case, the resource file is most likely not saved as a Unicode (or UTF8) file. 在您的情况下,资源文件很可能不会另存为Unicode(或UTF8)文件。 Previous versions of Visual Studio saved files (including web pages) using the system's locale by default which resulted in some pretty interesting problems for non-US developers. Visual Studio的早期版本默认情况下使用系统的语言环境保存文件(包括网页),这对于非美国开发人员来说会引起一些非常有趣的问题。

Examine the string returned by HttpContext.GetGlobalResourceObject("Resource", "SampleText").ToString() . 检查HttpContext.GetGlobalResourceObject("Resource", "SampleText").ToString()返回的字符串。 The content will probably be garbled, which means the original string was not saved as Unicode. 内容可能会出现乱码,这意味着原始字符串保存为Unicode。

The solution may be as simple as converting the resource file to Unicode. 该解决方案可能像将资源文件转换为Unicode一样简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM