I have an external vendor API that returns JSON in this format
{"3":{"id":1,"name":"Scott Foo","age":55},
"59":{"id":2,"name":"Jim Morris","age":62}}
I am trying to deserialize it using the following code
[DataContract]
public class Name
{
[DataMember]
public int id { get; set; }
[DataMember]
public string name { get; set; }
[DataMember]
public int age{ get; set; }
}
Code to deserialize is
List<Name> nameList = Deserialize<List<Name>>(temp);
where the Deserialize is defined as
public static T Deserialize<T>(string json)
{
T obj = Activator.CreateInstance<T>();
MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json));
DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
obj = (T)serializer.ReadObject(ms);
ms.Close();
ms.Dispose();
return obj;
}
The object returned in nameList is of zero count. Any idea how this JSON can be deserialized in .Net (i.e. not using Json.Net or any such third-party dll)?
Deserialize<Dictionary<string, Name>>- since that's not a list/JSON array, it's a JSON object with two properties named "3" and "59".