Answer:
I was able to figure out my own problem. I think the issue had to do with the fact that my Json was in an array format, but I didn't realize that.
I attempted to use a different serialization class by following the person who posted here. The new class threw an exception indicating it wasn't prepared to deserialize arrays, or something to that effect.
Here's the resulting code that worked:
return new JavaScriptSerializer().Deserialize<IList<HubspotObject>>(inputContent);
Original question:
I'm attempting to deserialize data from a hubspot json feed for my first time into a concrete object. I'm using the example from this codespot project to help get the job done.
I'm receiving all null properties after attempting to deserialize the code, and I'm simply not sure how to trouble shoot this.
Here is an example of the json:
[{
"blogTitle":"Practice blog",
"feedUrl":"http://feeds2.feedburner.com/asdf",
"guid":"asdf5-33f2-4a32-9495-8cd93f1f8252",
"jsonUrl":"https://hubapi.com/blog/v1/asdf-33f2-4a32-9495-8cd93f1f8252.json",
"moderated":false,
"moderators":["[email protected]"],
"portalId":42494,"webUrl":"blog.asdf.com/blog"
}]
Here is my class:
using System;
using System.Runtime.Serialization;
namespace Json
{
[DataContract()]
public class HubspotObject : IExtensibleDataObject
{
[DataMember(Name = "authorDisplayName")]
public string AuthorDisplayName { get; set; }
[DataMember(Name = "authorEmail")]
public string Link { get; set; }
[DataMember(Name = "blogGuid")]
public string BlogId { get; set; }
[DataMember(Name = "body")]
public string Body { get; set; }
[DataMember(Name = "createTimestamp")]
public int TimeStamp { get; set; }
[DataMember(Name = "draft")]
public bool Draft { get; set; }
[DataMember(Name = "guid")]
public string Guid { get; set; }
[DataMember(Name = "jsonUrl")]
public string JsonUrl { get; set; }
[DataMember(Name = "lastUpdateTimestamp")]
public int LastUpdate { get; set; }
[DataMember(Name = "metaDescription")]
public string MetaDescription { get; set; }
[DataMember(Name = "metaKeywords")]
public string MetaKeywords { get; set; }
[DataMember(Name = "portalId")]
public int PortalId { get; set; }
[DataMember(Name = "postAnalytics")]
public PostAnalytics PostAnalytics { get; set; }
[DataMember(Name = "publishTimestamp")]
public int PublishTimestamp { get; set; }
[DataMember(Name = "sendNotifications")]
public bool SendNotifications { get; set; }
[DataMember(Name = "summary")]
public string Summary { get; set; }
[DataMember(Name = "tags")]
public string[] Tags { get; set; }
[DataMember(Name = "title")]
public string Title { get; set; }
[DataMember(Name = "url")]
public string Url { get; set; }
// WCF stores any items we did not map here
public ExtensionDataObject ExtensionData { get; set; }
}
[DataContract()]
public class PostAnalytics : IExtensibleDataObject
{
[DataMember(Name = "comments")]
public int Comments { get; set; }
[DataMember(Name = "inboundLinks")]
public int InboundLinks { get; set; }
[DataMember(Name = "views")]
public int Views { get; set; }
// WCF stores any items we did not map here
public ExtensionDataObject ExtensionData { get; set; }
}
}
And finally, here's the line where I deserialize:
using (MemoryStream stream = new MemoryStream(Encoding.Unicode.GetBytes(inputContent)))
{
// Convert the stream buffer to an object with our serializer.
return serializer.ReadObject(stream) as HubspotObject;
}
Again, all of the objects are null in this after deserialization. There are no exceptions thrown. If I set the attribute of one of the properties to "required", it throws an exception when it does not populate, which makes me think I'm parsing it wrong, somehow.
Suggestions? Thanks in advance.