Help with deserializing to IEnumerable<T>

Jun 5, 2011 at 5:38 AM

Hello,

..The following is for a Windows Phone 7 project..

I am new to working with Json data and Json.NET. I am attempting to deserialize the following Json data:

 

{
  "realms":[
    {
      "type":"pvp",
      "queue":false,
      "status":true,
      "population":"high",
      "name":"Blackrock",
      "slug":"blackrock"
    },
    {
      "type":"pve",
      "queue":false,
      "status":true,
      "population":"medium",
      "name":"Medivh",
      "slug":"medivh"
    }
  ]
}

I have created a class in my project:

 

    public class Realm
    {
        public string name { get; set; }
        public string slug { get; set; }
        public string type { get; set; }
        public bool status { get; set; }
        public bool queue { get; set; }
        public string population { get; set; }
    }

 

And below is the code I am using to attempt to deserialize the Json data to an IEnumerable<Realm>:

 

    var results = JsonConvert.DeserializeObject<Dictionary<string, object>>(s);
    *var realmList = (IEnumerable<Realm>)(results["realms"]);
    foreach (Realm r in realmList)
    {
          this.Items.Add(new ItemViewModel() { LineOne = r.name, LineTwo = r.status.ToString().ToUpper(), LineThree = r.type.ToUpper() });
    }

*However, at this line, I receive an InvalidCastException. If I replace (IEnumerable<Realm>) with (IEnumerable<JToken>), there is no longer an InvalidCastException being thrown.

Any words of advice or obvious parts I am missing out on?

Thanks!