【问题标题】:Deserialize JSON into custom object using .NET使用 .NET 将 JSON 反序列化为自定义对象
【发布时间】:2012-01-19 17:33:10
【问题描述】:

答案:

我能够找出我自己的问题。我认为这个问题与我的 Json 是数组格式有关,但我没有意识到这一点。

我试图通过关注发布here 的人来使用不同的序列化类。新类抛出了一个异常,表明它没有准备好反序列化数组,或者类似的东西。

这是运行的结果代码:

return new JavaScriptSerializer().Deserialize<IList<HubspotObject>>(inputContent);

原问题:

我第一次尝试将来自 hubspot json 提要的数据反序列化为具体对象。我正在使用来自 this codespot project 的示例来帮助完成工作。

我在尝试反序列化代码后收到所有 null 属性,我只是不知道如何解决这个问题。

这是一个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":["asdf@asdf.com"],
    "portalId":42494,"webUrl":"blog.asdf.com/blog"
}]

这是我的课:

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

}

最后,这是我反序列化的行:

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

再一次,反序列化后 this 中的所有对象都为空。没有抛出异常。如果我将其中一个属性的属性设置为“必需”,它会在未填充时引发异常,这让我觉得我解析错了,不知何故。

建议?提前致谢。

【问题讨论】:

    标签: .net json serialization


    【解决方案1】:

    我能够找出我自己的问题。我认为这个问题与我的 Json 是数组格式有关,但我没有意识到这一点。

    我试图通过关注发布here 的人来使用不同的序列化类。新类抛出了一个异常,表明它没有准备好反序列化数组,或者类似的东西。

    这是有效的结果代码:

    return new JavaScriptSerializer().Deserialize<IList<HubspotObject>>(inputContent);
    

    【讨论】:

      【解决方案2】:

      几周前我在反序列化方面苦苦挣扎,这里有人推荐使用 Json.net

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

      我发现它很容易安装并且可以正常工作,我认为通过您的简单 json 示例,您应该能够轻松地使用 json.net 只需几行代码就可以反序列化它

      【讨论】:

      • 感谢您的建议。我刚刚得到了这个例子,但我还没有学到太多东西。我去看看 json-net 看看它是怎么回事。
      【解决方案3】:

      试试这个:

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

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

      我遇到过类似的使用流读取xml的情况:它在using语句中读取整个流,所以你必须将流的引用点设置回开头,然后再尝试将其读入对象。

      【讨论】:

      • 感谢您的回复,我试了一下没有效果。我对更多想法感兴趣,但我仍然不确定如何解决这个问题,我对这些课程非常陌生。
      猜你喜欢
      • 2022-11-15
      • 1970-01-01
      • 1970-01-01
      • 2013-07-25
      • 1970-01-01
      • 1970-01-01
      • 2017-01-28
      • 2012-03-07
      • 2021-06-24
      相关资源
      最近更新 更多