So I am trying to parse some JSON that is returned to me by a third party api that looks something like:
{
"status":"ok",
"links":
[
{
"link":
{
"link_name":"Sample",
"link_id":"9999"
}
},
],//and so on with other nested properties
I have created classes to map the JSON to
[DataContract]
public class JsonTestResults
{
[DataMember]
public string status { get; set; }
[DataMember]
public IEnumerable<Link> links { get; set; }
}
[DataContract]
public class Link
{
[DataMember]
public string link_name { get; set; }
[DataMember]
public string link_id { get; set; }
}
And I'm pushing the response through this deserializer (taken from this post
public T Deserialise<T>( string json )
{
T obj = Activator.CreateInstance<T>( );
using (MemoryStream ms = new MemoryStream( Encoding.Unicode.GetBytes( json ) ))
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer( obj.GetType( ) );
obj = (T)serializer.ReadObject( ms );
return obj;
}
}
However, my deserialized results are showing the contents of Link[] as null. (there is a Link object for each one returned, but the link_name and link_id are null.)
I've checked out this, this, this, this and this, but haven't been able to solve this issue. I am looking for a solution that doesn't require a third party library. (per my lead dev).
I don't believe it's a problem with the classes matching the JSON, but I can post the full code if anyone would like to review it.