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

时间:2019-04-18 19:52:12

标签: json vb.net winforms azure azure-cognitive-services

我正在Windows窗体中的Visual Studio 2017中构建程序-抱歉,但这是我唯一会使用的方法-无论如何,大多数与此有关的都是C#,所以我一直在寻求帮助方面遇到困难。

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

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

  

Newtonsoft.Json.JsonSerializationException:
  '不能反序列化   当前的JSON对象(例如{“ name”:“ value”})   System.Collections.Generic.List 1[System.Collections.Generic.Dictionary 2 [System.String,System.Collections.Generic.List 1[System.Collections.Generic.Dictionary 22 [System.String,System.String]]]]''   因为该类型需要JSON数组(例如[1,2,3])进行反序列化   正确。
  要解决此错误,请将JSON更改为JSON数组   (例如[1,2,3])或更改反序列化类型,使其成为常规   .NET类型(例如,不是整数之类的原始类型,不是集合   可以从JSON反序列化的类型(如数组或列表)   宾语。也可以将JsonObjectAttribute添加到类型中以强制它   从JSON对象反序列化。路径“错误”,第1行,位置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"
         }
      ]
   }
]

1 个答案:

答案 0 :(得分:0)

好!!!在解决这个问题之后-Jimi-您的解决方案成功了!!!非常感谢!我必须将以下内容删除到行:request.Headers.Add(“ Ocp-Apim-Subscription-Region”,“ westus”)request.Headers.Add(“ X-ClientTraceId”,Guid.NewGuid()。ToString() )