10

I'm using the Yahoo fantasy sports api. I'm getting a result like this:

"player": [
    {
        ...
        "eligible_positions": {
            "position": "QB"
        },
        ...
    },
    {
        ...
        "eligible_positions": {
            "position": [
                "WR",
                "W/R/T"
            ]
        },
        ...
    },

How is it that I can deserialize this?

My code looks like this:

var json = new JavaScriptSerializer();

if (response != null)
{
    JSONResponse JSONResponseObject = json.Deserialize<JSONResponse>(response);
    return JSONResponseObject;
}

And in my 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 I run this, since eligible_positions can return both a string and a string array, I keep getting the error "Type 'System.String' is not supported for deserialization of an array".

I've also tried turning public string position { get; set; } to public string[] position { get; set; } but I still get an error.

How should I handle this?

6
  • 1
    What is the error when you use string[]? Have you tried List<string> ? Commented Feb 26, 2014 at 20:30
  • 2
    @IainBallard: That won't help. Commented Feb 26, 2014 at 20:31
  • Yes. When I use string[], I get "System.String' to type 'System.String[]". When I use List<string>, I get "Cannot convert object of type 'System.String' to type 'System.Collections.Generic.List`1[System.String]'"; Commented Feb 26, 2014 at 20:33
  • 1
    I think this one may help you. stackoverflow.com/questions/10141610/… Commented Feb 26, 2014 at 20:50
  • @user850237 Are you open to use Json.Net? Commented Feb 26, 2014 at 21:03

1 Answer 1

16

I'll use Json.Net. The idea is: "declare position as a List<string> and if the value in json is a string. then convert it to a List"

Code to 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 This
    public List<string> position { get; set; }
}

public class Player
{
    public EligiblePositions eligible_positions { get; set; }
}

public class SportsAPI
{
    public List<Player> player { get; set; }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Awesome. This was nice and easy and works perfectly. Thanks!
Hey L.B Can you help me out with this . The reader.value is null and I m looking at List<Class> rather than List<string> stackoverflow.com/questions/31486801/…
Is it best to add CanWrite if you don't implement WriteJson public override bool CanWrite { get { return false; } }

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.