用希伯来字母反序列化 json

deserializing json with hebrew letters

我正在尝试反序列化一个包含希伯来语单词的 json 文件。 我正在使用 .net 核心。

这是我的一部分 json:

{

    "MandatoryWords": {
        "select": {
            "sql": "select",
            "oracle": "select",
            "Hebrew": [ "בחר", "הצג" ]
        },

        "from": {
            "sql": "from",
            "oracle": "from",
            "Hebrew": [ "מ", "מתוך", "של" ]
        },

        "where": {
            "sql": "where",
            "oracle": "where",
            "Hebrew": [ "כש", "בתנאי ש", "אם", "כאשר", "לכל מי ש", "רק עבור", "עבור" ]
        }
   }
}

我为每个部分构建了这个class

public class JsonPart
{
    public string sql { get; set; }
    public string oracle { get; set; }
    public List<string> Hebrew { get; set; }
}

当我使用这段代码时:

        var options = new JsonSerializerOptions
        {
            PropertyNameCaseInsensitive = true,
        };
        Dictionary < string,Dictionary<string,JsonPart>> j;
        var myJsonString = File.ReadAllText(@"C:\Users\Desktop\ConsoleApp1\ConsoleApp1\Substitutes.json");
        j = JsonSerializer.Deserialize<Dictionary<string, Dictionary<string, JsonPart>>>(myJsonString);

字典中没有希伯来语单词,而是有问号 如下图所示

如果我使用此代码:

        var options = new JsonSerializerOptions
        {
            PropertyNameCaseInsensitive = true,
        };
        Dictionary < string,Dictionary<string,JsonPart>> j;
        var myJsonString = File.ReadAllBytes(@"C:\Users\Desktop\ConsoleApp1\ConsoleApp1\Substitutes.json");

        var utf8Reader = new Utf8JsonReader(myJsonString);
        j = JsonSerializer.Deserialize<Dictionary<string, Dictionary<string, JsonPart>>>(ref utf8Reader);

我收到这个错误:

System.Text.Json.JsonException
  HResult=0x80131500
  Message=The JSON value could not be converted to System.Collections.Generic.List`1[System.String]. Path: $.MandatoryWords.select.Hebrew[0] | LineNumber: 6 | BytePositionInLine: 29.
  Source=System.Text.Json
  StackTrace:
   at System.Text.Json.ThrowHelper.ReThrowWithPath(ReadStack& readStack, Utf8JsonReader& reader, Exception ex)
   at System.Text.Json.JsonSerializer.ReadCore(JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& readStack)
   at System.Text.Json.JsonSerializer.ReadValueCore(JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& readStack)
   at System.Text.Json.JsonSerializer.ReadValueCore(Utf8JsonReader& reader, Type returnType, JsonSerializerOptions options)
   at System.Text.Json.JsonSerializer.Deserialize[TValue](Utf8JsonReader& reader, JsonSerializerOptions options)
   at Simple.MyConverter.Converter() in C:\Users\Desktop\ConsoleApp1\ConsoleApp1\MyConverter .cs:line 31
   at ConsoleApp1.Program.Main(String[] args) in C:\Users\Desktop\ConsoleApp1\ConsoleApp1\Program.cs:line 10

  This exception was originally thrown at this call stack:
    [External Code]

Inner Exception 1:
InvalidOperationException: Cannot transcode invalid UTF-8 JSON text to UTF-16 string.

Inner Exception 2:
DecoderFallbackException: Unable to translate bytes [E1] at index 0 from specified code page to Unicode.

我想要一个解决方案。

在记事本中打开文件 Substitutes.json 并用 UTF-8 编码保存,我测试过,用 ANSI 或 UTF-16 保存后它不起作用

检查文件的编码。 然后阅读:

var json = File.ReadAllText("path_to_file", "file_encoding");