2

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.

0

3 Answers 3

1

Try this:

   // Set the position to the beginning of the stream.
   stream.Seek(0, SeekOrigin.Begin);
   return serializer.ReadObject(stream) as HubspotObject;

from here: http://msdn.microsoft.com/en-us/library/system.io.memorystream%28v=vs.71%29.aspx

I have encountered similar occurences using streams to read xml: it reads the whole stream in the using statement, so you have to set the reference point of the stream back to the beginning before trying to read it into an object.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your response, I gave it a shot to no effect. I'm interested in any more ideas, I'm still not sure how to trouble shoot this issue, I'm so unfamiliar with these classes.
1

I was struggling with deserialization a few weeks back and someone on here recommended using Json.net

http://james.newtonking.com/projects/json-net.aspx

I found it easy to install and got it working, I think with your simple json example you should easily be able to deserialize it with just a few lines of code using json.net

1 Comment

Thanks for your suggestion. I just got this example working, but I haven't learned too much yet. I'll check out json-net and see what it's all about.
0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.