简体   繁体   English

C# JSON 西班牙文是 UTF8 但德文是 UTF7 加载

[英]C# JSON Spanish is UTF8 but German is UTF7 to load

I'm building a website that is to have specific languages setup.我正在建立一个设置特定语言的网站。 All pages and their text are in one JSON per language.所有页面及其文本都在每种语言的一个 JSON 中。 I'm using the browser Server Variable to tell what language is if we have the JSON conversion for it, we display the text in their language.我正在使用浏览器服务器变量来判断语言是什么,如果我们有 JSON 转换,我们会以他们的语言显示文本。 Default is English if we don't cover it.如果我们不涵盖它,则默认为英语。

However, we load the data with the following code and it works for English and Spanish.但是,我们使用以下代码加载数据,它适用于英语和西班牙语。

byte[] bytes = File.ReadAllBytes(JSONFilePath);
string someString = Encoding.UTF8.GetString(bytes);
RecordData = (DataSet)JsonConvert.DeserializeObject(someString, (typeof(DataSet)));

However, we noticed it doesn't work for German.但是,我们注意到它不适用于德语。 We found that we have to use UTF7 for German.我们发现德语必须使用 UTF7。

byte[] bytes = File.ReadAllBytes(JSONFilePath);
string someString = Encoding.UTF7.GetString(bytes);
RecordData = (DataSet)JsonConvert.DeserializeObject(someString, (typeof(DataSet)));

If we use UTF8 for German, we get symbols, in some characters.如果我们对德语使用 UTF8,我们会得到一些字符中的符号。 If use UTF7 for Spanish the same thing will happen.如果将 UTF7 用于西班牙语,同样的事情也会发生。 We are setting up 10 or so languages, so how do we know which to encode the data with before we deserialize it?我们正在设置 10 种左右的语言,那么在反序列化之前我们如何知道用哪种语言对数据进行编码呢?

You don't need to encode files for different languages in different code pages.您不需要在不同的代码页中为不同的语言编码文件。 You can, but you will only make your life harder for no good reason.你可以,但你只会无缘无故地让你的生活变得更艰难。 You will need to maintain the mapping between your codepages of choice and your files yourself.您需要自己维护您选择的代码页和文件之间的映射。

Unicode covers all languages. Unicode涵盖所有语言。 UTF-8 supports all of Unicode, as do all Unicode encodings. UTF-8 支持所有 Unicode,所有 Unicode 编码也是如此。

Make sure you always save all your files as UTF-8, and make sure you always provide an encoding when reading from these files, at which point your code should be:确保始终将所有文件保存为 UTF-8,并确保在读取这些文件时始终提供编码,此时您的代码应为:

string someString = File.ReadAllText(JSONFilePath, Encoding.UTF8);
RecordData = (DataSet)JsonConvert.DeserializeObject(someString, (typeof(DataSet)));

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

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