問題描述
我在使用 JSON.NET 庫反序列化從 Facebook 返回的數據時遇到了一些麻煩.
I'm having a bit of trouble deserializing data returned from Facebook using the JSON.NET libraries.
從一個簡單的墻貼返回的 JSON 如下所示:
The JSON returned from just a simple wall post looks like:
{
"attachment":{"description":""},
"permalink":"http://www.facebook.com/permalink.php?story_fbid=123456789"
}
為照片返回的 JSON 如下所示:
The JSON returned for a photo looks like:
"attachment":{
"media":[
{
"href":"http://www.facebook.com/photo.php?fbid=12345",
"alt":"",
"type":"photo",
"src":"http://photos-b.ak.fbcdn.net/hphotos-ak-ash1/12345_s.jpg",
"photo":{"aid":"1234","pid":"1234","fbid":"1234","owner":"1234","index":"12","width":"720","height":"482"}}
],
一切都很好,我沒有問題.我現在遇到了一個來自移動客戶端的簡單墻貼,其中包含以下 JSON,而反序列化現在因這一個帖子而失敗:
Everything works great and I have no problems. I've now come across a simple wall post from a mobile client with the following JSON, and deserialization now fails with this one single post:
"attachment":
{
"media":{},
"name":"",
"caption":"",
"description":"",
"properties":{},
"icon":"http://www.facebook.com/images/icons/mobile_app.gif",
"fb_object_type":""
},
"permalink":"http://www.facebook.com/1234"
這是我反序列化的類:
public class FacebookAttachment
{
public string Name { get; set; }
public string Description { get; set; }
public string Href { get; set; }
public FacebookPostType Fb_Object_Type { get; set; }
public string Fb_Object_Id { get; set; }
[JsonConverter(typeof(FacebookMediaJsonConverter))]
public List<FacebookMedia> { get; set; }
public string Permalink { get; set; }
}
如果不使用 FacebookMediaJsonConverter,我會收到錯誤消息:無法將 JSON 對象反序列化為類型System.Collections.Generic.List`1[FacebookMedia]".這是有道理的,因為在 JSON 中,Media 不是一個集合.
Without using the FacebookMediaJsonConverter, I get an error: Cannot deserialize JSON object into type 'System.Collections.Generic.List`1[FacebookMedia]'. which makes sense, since in the JSON, Media is not a collection.
我發現這篇文章描述了一個類似的問題,所以我試圖走這條路:反序列化JSON,有時value是一個數組,有時是"(空字符串)
I found this post which describes a similar problem, so I've attempted to go down this route: Deserialize JSON, sometimes value is an array, sometimes "" (blank string)
我的轉換器看起來像:
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.StartArray)
return serializer.Deserialize<List<FacebookMedia>>(reader);
else
return null;
}
效果很好,只是我現在遇到了一個新異常:
Which works fine, except I now get a new exception:
在 JsonSerializerInternalReader.cs 內部,CreateValueInternal():反序列化對象時出現意外令牌:PropertyName
Inside JsonSerializerInternalReader.cs, CreateValueInternal(): Unexpected token while deserializing object: PropertyName
reader.Value 的值是永久鏈接".我可以在 switch 中清楚地看到 JsonToken.PropertyName 沒有案例.
The value of reader.Value is "permalink". I can clearly see in the switch that there's no case for JsonToken.PropertyName.
我需要在轉換器中做一些不同的事情嗎?謝謝你的幫助.
Is there something I need to do differently in my converter? Thanks for any help.
推薦答案
JSON.NET 的開發人員最終幫助了項目 codeplex 網站.這是解決方案:
The developer of JSON.NET ended up helping on the projects codeplex site. Here is the solution:
問題是,當它是一個 JSON 對象時,我沒有讀取過去的屬性.這是正確的代碼:
The problem was, when it was a JSON object, I wasn't reading past the attribute. Here is the correct code:
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.StartArray)
{
return serializer.Deserialize<List<FacebookMedia>>(reader);
}
else
{
FacebookMedia media = serializer.Deserialize<FacebookMedia>(reader);
return new List<FacebookMedia>(new[] {media});
}
}
James 也很友好地為上述方法提供了單元測試.
James was also kind enough to provide unit tests for the above method.
這篇關于有時是數組有時是對象時反序列化JSON的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!