c# - How to handle json that returns both a string and a string array? -


this question has answer here:

i'm using yahoo fantasy sports api. i'm getting result this:

"player": [     {         ...         "eligible_positions": {             "position": "qb"         },         ...     },     {         ...         "eligible_positions": {             "position": [                 "wr",                 "w/r/t"             ]         },         ...     }, 

how can deserialize this?

my code looks this:

var json = new javascriptserializer();  if (response != null) {     jsonresponse jsonresponseobject = json.deserialize<jsonresponse>(response);     return jsonresponseobject; } 

and in jsonresponse.cs file:

public class player {     public string player_key { get; set; }     public string player_id { get; set; }     public string display_position { get; set; }             public selectedposition selected_position { get; set; }     public eligible_positions eligible_positions { get; set; }     public name name { get; set; }             }   public class eligible_positions {             public string position { get; set; } } 

when run this, since eligible_positions can return both string , string array, keep getting error "type 'system.string' not supported deserialization of array".

i've tried turning public string position { get; set; } public string[] position { get; set; } still error.

how should handle this?

i'll use json.net. idea is: "declare position list<string> , if value in json string. convert list"

code deserialize

var api = jsonconvert.deserializeobject<sportsapi>(json); 

jsonconverter

public class stringconverter : jsonconverter {     public override bool canconvert(type objecttype)     {         throw new notimplementedexception();     }      public override object readjson(newtonsoft.json.jsonreader reader, type objecttype, object existingvalue, newtonsoft.json.jsonserializer serializer)     {          if(reader.valuetype==typeof(string))         {             return new list<string>() { (string)reader.value };         }         return serializer.deserialize<list<string>>(reader);     }      public override void writejson(newtonsoft.json.jsonwriter writer, object value, newtonsoft.json.jsonserializer serializer)     {         throw new notimplementedexception();     } } 

sample json

{     "player": [         {             "eligible_positions": {                 "position": "qb"             }         },         {             "eligible_positions": {                 "position": [                     "wr",                     "w/r/t"                 ]             }         }     ] }    

classes (simplified version)

public class eligiblepositions {     [jsonconverter(typeof(stringconverter))] // <-- see     public list<string> position { get; set; } }  public class player {     public eligiblepositions eligible_positions { get; set; } }  public class sportsapi {     public list<player> player { get; set; } } 

Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -