I have a json string that is being returned from HttpRequest that I am trying to deserialize to an object. The json has a root element that is not needed in my case (other applications that use the same data, need it so it can't be removed). I have tried several different ways to do this but my object is always null. I can see in the watch window that the json is being returned from the request correctly. Any ideas as to what I am missing?
My code is below.
Here is my object I am trying to deserialize to.
public class BrandHeaderResponse
{
public BrandHeaderData brandHeaderData { get; set; }
}
public class BrandHeaderData
{
public dynamic Image { get; set; } //url and alt text
public string BackgroundColor { get; set; }
public string LiveText { get; set; }
}
Here is the code
HttpResponseMessage response;
using (var request = new HttpRequestMessage(HttpMethod.Get, new Uri(url)))
{
response = await webClient.SendAsync(request, requestHeaders);
}
using (var stream = await response.Content.ReadAsStreamAsync())
{
using (var sr = new StreamReader(stream))
{
using (var reader = new JsonTextReader(sr))
{
var serializer = new JsonSerializer();
var data = serializer.Deserialize<T>(reader);
return data;
}
}
}
And lastly, here is the json.
{
"2000_banner":{
"ComponentName":"2000_banner",
"SchemaName":"Brand Banner",
"BrandName":"Rockport - Dummy Banner",
"LogoTextColor":"Dark",
"Image":{
"ImageUrl":"http://n.media.com/staging/24?w=153&h=64",
"AltText":"Burberry"
},
"LiveText":"This is dummy brand text for Rockport.",
"BackgroundColor":"E3D9CE"
}
}
"2000_banner", or can the property name change? 2) Can there be other properties in the root object?