I would look at the Json you are receiving from the Translation Service. The error sounds like you are only getting one TranslationResult instead of an array of TranslationResult. Put a breakpoint on line 1159 and look at what result is.
You might have to copy result into the clipboard and use paste special --> paste json as classes to update the class you are deserializing into
Microsoft Translation API for Winform
Inventerio Soft
1
Reputation point
I'm trying to use the Microsoft Translation API in VB.Net, I'm using the Microsoft sample codes (I converted the code from C# to VB.Net), it was working fine until a month ago, but now, it's giving an error.
Does anyone know the solution for this?
Thanks in advance
The code I used:
Private Async Sub ArabicTranslation()
Dim route As String = "/translate?api-version=3.0&to=ar"
Dim textToTranslate As String = txtProductName.Text
Dim body As Object() = New 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(endpoint_var & route)
request.Content = New StringContent(requestBody, Encoding.UTF8, "application/json")
request.Headers.Add("Ocp-Apim-Subscription-Key", key_var)
request.Headers.Add("Ocp-Apim-Subscription-Region", region_var)
Dim response As HttpResponseMessage = Await client.SendAsync(request).ConfigureAwait(False)
Dim result As String = Await response.Content.ReadAsStringAsync()
Dim deserializedOutput As TranslationResult() = JsonConvert.DeserializeObject(Of TranslationResult())(result)
For Each o As TranslationResult In deserializedOutput
For Each t As Translation In o.Translations
txtArabicName.Text = t.Text
Next
Next
End Using
End Using
End Sub
1 answer
Sort by: Most helpful
-
Ken Tucker 5,851 Reputation points
2021-02-27T11:15:26.517+00:00