dave317
dave317

Reputation: 774

JSON Deserialization of non root object

This is what my JSON data looks like, at least for two records:

{"version":"1.0","players":{"timestamp":"1532153048","since":"Wed Dec 31 7:00:00 p.m. ET 1969","player":[{"draft_year":"2010","draft_round":"6","nfl_id":"antoniobrown/2508061","rotoworld_id":"5698","stats_id":"24171","position":"WR","stats_global_id":"406214","espn_id":"13934","kffl_id":"22341","weight":"181","id":"9988","birthdate":"584514000","draft_team":"PIT","name":"Brown, Antonio","draft_pick":"26","college":"Central Michigan","height":"70","jersey":"84","twitter_username":"AntonioBrown84","sportsdata_id":"16e33176-b73e-49b7-b0aa-c405b47a706e","team":"PIT","cbs_id":"1272852"},{"draft_year":"2018","draft_round":"1","rotoworld_id":"13048","status":"R","stats_id":"30972","position":"RB","stats_global_id":"883302","kffl_id":"37867","weight":"230","id":"13604","draft_team":"NYG","birthdate":"855291600","name":"Barkley, Saquon","draft_pick":"2","college":"Penn State","height":"71","jersey":"26","team":"NYG","cbs_id":"2185957"}]},"encoding":"utf-8"}

I want to deserialize this into a list, for now I'll just take a couple fields. This is my model:

public class MflPlayerJSON
    {
            [JsonProperty(PropertyName = "id")]
            public string ID { get; set; }

            [JsonProperty(PropertyName = "stats_id")]
            public string YahooID { get; set; }

            [JsonProperty(PropertyName = "draft_year")]
            public string DraftYear { get; set; }

            [JsonProperty(PropertyName = "team")]
            public string Team { get; set; }

            [JsonProperty(PropertyName = "name")]
            public string Name { get; set; }
}

Here is my deserilization code: From tests I have found I am not deserialilzing properly but I dont know how to do this. I am trying to avoid having multiple models, but will do that if i need to:

var json = await response.Content.ReadAsStringAsync();
                ViewBag.Msg2 = json.ToString();

                var jObject = JObject.Parse(json);
                // list = JsonConvert.DeserializeObject<MflPlayerJSON>(json);
                var s = jObject.Count;
                if (jObject.ContainsKey("players"))
                {
                    ViewBag.Msg = s;
                    list = jObject["players"].ToObject<List<MflPlayerJSON>>();
                }

Error message is

JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 

I am so close! Please help

Upvotes: 2

Views: 1073

Answers (1)

abydal
abydal

Reputation: 388

Here is an alternative solution where you create the entire model hierarchy. I put the json data in a .json file to avoid all the escaping of quotation marks

public class APIResult 
{
    [JsonProperty(PropertyName = "players")]
    public PlayerData PlayerData {get; set;}
}
public class PlayerData
{
    [JsonProperty(PropertyName = "player")]
    public List<MflPlayerJSON> Players {get; set;}
}

public class MflPlayerJSON
{
    [JsonProperty(PropertyName = "id")]
    public string ID { get; set; }

    [JsonProperty(PropertyName = "stats_id")]
    public string YahooID { get; set; }

    [JsonProperty(PropertyName = "draft_year")]
    public string DraftYear { get; set; }

    [JsonProperty(PropertyName = "team")]
    public string Team { get; set; }

    [JsonProperty(PropertyName = "name")]
    public string Name { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        var json = File.ReadAllText("sample.json");
        var o = JsonConvert.DeserializeObject<APIResult>(json);

    }
}

Upvotes: 2

Related Questions