Note that there are some explanatory texts on larger screens.

plurals
  1. PODeserializing JSON when sometimes array and sometimes object
    text
    copied!<p>I'm having a bit of trouble deserializing data returned from Facebook using the JSON.NET libraries.</p> <p>The JSON returned from just a simple wall post looks like:</p> <pre><code>{ "attachment":{"description":""}, "permalink":"http://www.facebook.com/permalink.php?story_fbid=123456789" } </code></pre> <p>The JSON returned for a photo looks like:</p> <pre><code>"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"}} ], </code></pre> <p>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:</p> <pre><code>"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" </code></pre> <p>Here is the class I am deserializing as:</p> <pre><code>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&lt;FacebookMedia&gt; { get; set; } public string Permalink { get; set; } } </code></pre> <p>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. </p> <p>I found this post which describes a similar problem, so I've attempted to go down this route: <a href="https://stackoverflow.com/questions/4572732/deserialize-json-sometimes-value-is-an-array-sometimes-blank-string">Deserialize JSON, sometimes value is an array, sometimes &quot;&quot; (blank string)</a></p> <p>My converter looks like:</p> <pre><code>public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.TokenType == JsonToken.StartArray) return serializer.Deserialize&lt;List&lt;FacebookMedia&gt;&gt;(reader); else return null; } </code></pre> <p>Which works fine, except I now get a new exception:</p> <p>Inside JsonSerializerInternalReader.cs, CreateValueInternal(): Unexpected token while deserializing object: PropertyName</p> <p>The value of reader.Value is "permalink". I can clearly see in the switch that there's no case for JsonToken.PropertyName. </p> <p>Is there something I need to do differently in my converter? Thanks for any help. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload