Question

As a beginner I tried to deserialize Google transliterator return array. It's a JSON array like this:

[{"ew" : "namaste","hws" : ["नमस्ते","नमसते","नमास्ते",]},]

pretty awesome!

This is my c# class used for deserialization:

   [Serializable]
   public class googleTransliterator  
   {

       [JsonProperty("ew")]
       public String sourceWord { get; set; }

       [JsonProperty("hws")]
       public String[] transliteratedWords { get; set; }

   }

and finally:

using Newtonsoft.Json;
...
...
// return value from google transliteration API is in streamReader
var s = new JsonSerializer();
var gt = (googleTransliterator) s.Deserialize( new StringReader( streamReader.ReadToEnd()), typeof( googleTransliterator));

And what I got is:

Cannot create and populate list type GoogleTransliterator.googleTransliterator.

Any idea what is wrong?

Was it helpful?

Solution

The result is an array of JSON objects ([{...},]) instead of a plain object ({...}).

Your code should work if you change it like this:

var gt = ((googleTransliterator[])s.Deserialize(
    new StringReader(streamReader.ReadToEnd()),
    typeof(googleTransliterator[])))[0];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top