-1

Im doing a Microsoft Cognitive Services project with langauge detection and text translation.

Im receiving Json from the Microsoft Api, and i want to access it as an object. I've tried a few different things, including JsonConvert.DeserializeObject, and mapping it to my own objects. (Example below)

// Send the request and get response.
            HttpResponseMessage response = await client.SendAsync(request).ConfigureAwait(false);
            // Read response as a string.
            if (response.ReasonPhrase == "OK")
            {
                //string result = await response.Content.ReadAsStringAsync();
                Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(result); 

            }

// The response:

[{
 "detectedLanguage":
 {
    "language":"fi",
        "score":1.0
 },
  "translations": 
  [
     {"text":"This is a test, I hope it works. The text is in Finnish.","to":"en"},
     {"text":"Dette er en test, jeg håber det virker. Teksten er på finsk.","to":"da"}
  ]
}]

// I found an online tool to generate the appropriate classes for mapping

 public class DetectedLanguage
    {
        [JsonPropertyName("language")]
        public string Language { get; set; }

        [JsonPropertyName("score")]
        public double Score { get; set; }
    }

    public class Translation
    {
        [JsonPropertyName("text")]
        public string Text { get; set; }

        [JsonPropertyName("to")]
        public string To { get; set; }
    }

    public class MyArray
    {
        [JsonPropertyName("detectedLanguage")]
        public DetectedLanguage DetectedLanguage { get; set; }

        [JsonPropertyName("translations")]
        public List<Translation> Translations { get; set; }
    }

    public class Root
    {
        [JsonPropertyName("MyArray")]
        public List<MyArray> MyArray { get; set; }
    }

// The Error im getting

 Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'textananlytics_test.Root' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path '', line 1, position 1.'

2 Answers2

2

You can change

Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(result); 

to

List<MyArray> list = JsonConvert.DeserializeObject<List<MyArray>>(result); 
Root myDeserializedClass=new Root{MyArray=list};
Yiyi You
  • 16,875
  • 1
  • 10
  • 22
1

In your case, JSON must has next structure:

{
    "MyArray": [
        { /* MyArray jobject */ },
        { /* MyArray jobject */ },
        ...
    ]
}

So Yiyi You wrote correct processing

Den
  • 397
  • 3
  • 4