JSON Azure 翻译服务出现反序列化错误

JSON deserialization error with Azure translation services

我正在 Visual Studio 2017 中以 Windows 形式构建一个程序 - 抱歉,这是我唯一知道如何使用的东西 - 无论如何,大部分都是 C#,所以我遇到了麻烦获得帮助。

我已经翻译了 Microsoft 提供的 C# 程序示例以连接到 Azure 认知翻译服务、注册、获得我的所有密钥等。

当我 运行 代码时,出现以下错误:

Newtonsoft.Json.JsonSerializationException:
'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type System.Collections.Generic.List1[System.Collections.Generic.Dictionary2[System.String,System.Collections.Generic.List1[System.Collections.Generic.Dictionary2[System.String,System.String]]]]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'error', line 1, position 9.'

我尝试了太多不同来源的东西,无法一一列举。我对 JSON 了解不多,正在寻求有关解决上述问题的代码的帮助。

Public Class DetectedLanguage
    Public Property language As String
    Public Property score As Double
End Class

Public Class Translation
    Public Property text As String
    Public Property two As String
End Class

Public Class Example
    Public Property detectedLanguage As DetectedLanguage
    Public Property translations As Translation()
End Class

Dim textToTranslate As String = root
Dim fromLanguage As String
Dim fromLanguageCode As String = cabbr

Dim toLanguageCode As String = "en"

Dim endpoint As String = String.Format(TEXT_TRANSLATION_API_ENDPOINT, "translate")
Dim uri As String = String.Format(endpoint & "&from={0}&to={1}", fromLanguageCode, toLanguageCode)
Dim body As System.Object() = New System.Object() {New With {Key .Text = textToTranslate}}
Dim requestBody = JsonConvert.SerializeObject(body)

Using client = New HttpClient()
    Using request = New HttpRequestMessage()
        request.Method = HttpMethod.Post
        request.RequestUri = New Uri(uri)
        request.Content = New StringContent(requestBody, Encoding.UTF8, "application/json")
        request.Headers.Add("Ocp-Apim-Subscription-Key", COGNITIVE_SERVICES_KEY)
        request.Headers.Add("Ocp-Apim-Subscription-Region", "westus")
        request.Headers.Add("X-ClientTraceId", Guid.NewGuid().ToString())
        Dim response = client.SendAsync(request).Result
        Dim responseBody = response.Content.ReadAsStringAsync().Result
        Dim result = JsonConvert.DeserializeObject(Of List(Of Dictionary(Of String, List(Of Dictionary(Of String, String)))))(responseBody)
        Dim translation = result(0)("translations")(0)("text")
        rtRoot.Text = translation
    End Using
End Using

我已经使用 jsonutil 站点粘贴了我的 JSON 代码并获取了 类。

这是我的JSON内容:

[
   {
      "detectedLanguage":{
         "language":"nl",
         "score":1.0
      },
      "translations":[
         {
            "text":"bord vervangen en uitvoerig getest",
            "to":"nl"
         },
         {
            "text":"Board replaced and tested extensively",
            "to":"en"
         }
      ]
   }
]

好的!!!玩完这个之后 - Jimi - 你的解决方案奏效了!!!太感谢了!我不得不删除以下行:request.Headers.Add("Ocp-Apim-Subscription-Region", "westus") request.Headers.Add("X-ClientTraceId", Guid.NewGuid().ToString() )