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

微笑、不失礼 提交于 2019-12-11 20:00:41

问题


I want to deserialise a JSON object containing unicode data to string array. While the characters inside the JSON object is english, it works fine. 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

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"]
[“大小”,“姓名”,“類型”]

回答1:


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.

Strings in .NET are Unicode. It isn't possible to create a non-Unicode string.

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. The OS (and .NET) will use the system locale to read non-unicode files which can result in garbled data. 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.

In your case, the resource file is most likely not saved as a Unicode (or UTF8) file. 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.

Examine the string returned by HttpContext.GetGlobalResourceObject("Resource", "SampleText").ToString(). The content will probably be garbled, which means the original string was not saved as Unicode.

The solution may be as simple as converting the resource file to Unicode.




回答2:


Yes, You need to first convert resx value to unicode and then use that value to deserialize JSON value and then convert to string array...

Deserialize Json encountered URL change

http://social.msdn.microsoft.com/Forums/windowsapps/en-US/d31864e0-015d-4dd3-9207-176281cf6f7b/deserialize-json-in-winrt

http://www.codeproject.com/Articles/159450/fastJSON

Deserializing a simple JSON array with DataContractJsonSerializer



来源:https://stackoverflow.com/questions/25403588/jsonserializer-deserialize-not-working-properly-for-unicode-characters-in-c-shar

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!