0

There is a JSON array like below

[{ "name" : "XX", "age" : 20}, { "company" : "YY", "post" : "XXX"} , ...]

I want to deserialize it into object[], but I wanna control the actual type of the element in the array to be instance of specificed classes

class A{
  public string name;
  public int age;
}

class B{
  public string company;
  public string post;
}

So that I can get the object by (A)array[0] or (B)array(1)

Thx

Mr.Wang from Next Door
  • 13,670
  • 12
  • 64
  • 97
  • possible duplicate of [Deserializing heterogenous JSON array into covariant List<> using JSON.NET](http://stackoverflow.com/questions/8241392/deserializing-heterogenous-json-array-into-covariant-list-using-json-net) – Mr.Wang from Next Door Feb 15 '14 at 10:01

1 Answers1

0

I found it in this way

using( StringReader sr = new StringReader(json))
using( JsonTextReader reader = new JsonTextReader(sr) )
{
    JArray array = JArray.Load(reader);

    array[0].ToObject<A>();
    array[1].ToObject<B>();
}
Mr.Wang from Next Door
  • 13,670
  • 12
  • 64
  • 97