我能弄清楚我自己的問題。我認爲這個問題與我的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":["[email protected]"],
"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;
}
同樣,a在反序列化之後,這些對象中的所有對象都是null。沒有例外拋出。如果我將其中一個屬性的屬性設置爲「required」,它會在不填充時拋出一個異常,這使我認爲我解析它錯了,不知何故。
建議?提前致謝。
感謝您的答覆,我給它一個鏡頭,沒有效果。我對任何更多的想法感興趣,我仍然不確定如何解決這個問題,我對這些課程非常不熟悉。 – letsgetsilly 2011-12-13 22:56:23