Fredderf81
Fredderf81

Reputation: 29

JSON deserialization error with Azure translation services

I am building a program in Visual Studio 2017 in Windows Forms - sorry but that's the only thing I know how to use - anyway, most everything for this is C#, so I've been having trouble getting help.

I have translated the Microsoft provided example for a C# program to connect to Azure Cognitive Translation services, signed up, got all my keys, etc.

When I run the code, I get the following error:

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.'

I have tried too many things to list from many different sources. I do not know a whole lot about JSON and am asking for help with the code to solve the above issue.

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

I have already used the jsonutil site to paste my JSON code in and get the classes.

Here is my JSON content:

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

Upvotes: 1

Views: 944

Answers (1)

Fredderf81
Fredderf81

Reputation: 29

OK!!! after playing around with this - Jimi - your solution worked!!! thank you SO much! i had to remove the following to lines: request.Headers.Add("Ocp-Apim-Subscription-Region", "westus") request.Headers.Add("X-ClientTraceId", Guid.NewGuid().ToString())

Upvotes: 0

Related Questions