0

So I have some json I am trying to get into a referenceable object using the Newtonsoft Json library, but I am truly stumped on this one. Basically, my data looks like this:

{
    "43086": [{
        "name": "Karthus's Brigands",
        "tier": "SILVER",
        "queue": "RANKED_SOLO_5x5",
        "entries": [{
            "playerOrTeamId": "43086",
            "playerOrTeamName": "Testy Test",
            "division": "IV",
            "leaguePoints": 77,
            "wins": 130,
            "isHotStreak": false,
            "isVeteran": true,
            "isFreshBlood": false,
            "isInactive": false
        }]
    }]
}

When I try to create an object to hold this data, and use the DeserializeObject function, my object always ends up null, and there are no exceptions being thrown.

I tried creating the object by hand, but once I had trouble, I ran the returned json through json2csharp, it gives me this:

public class Entry

{

public string playerOrTeamId { get; set; }

public string playerOrTeamName { get; set; }

public string division { get; set; }

public int leaguePoints { get; set; }

public int wins { get; set; }

public bool isHotStreak { get; set; }

public bool isVeteran { get; set; }

public bool isFreshBlood { get; set; }

public bool isInactive { get; set; }

}

public class invalid_type43086

{

public string name { get; set; }

public string tier { get; set; }

public string queue { get; set; }

public List<Entry> entries { get; set; }

}

public class RootObject

{

public List<invalid_type43086> invalid_name43086 { get; set; }

}

I've had other issues with this API, where the objects arent recognized by that converter, so I cleaned those up, but still no dice.

This is the line of code I have for deserialization:

var leagueStats = JsonConvert.DeserializeObject<SummonerClasses.RootObject>(returnedData);

and as mentioned, leagueStats ends up null immediately after that. I have confirmed that returnedData actually holds the above json, so I am not sure what the problem is, especially since its not throwing any exceptions. Stepping through the code provided no insight.

I also tried using the PopulateObject method from the newtonsoft library, and building a dictionary, but that failed as well (I got an error about it not being able to serialize into a non-indexable object, but I am unsure what the specific error was because I blew away that code once it was clear it wasn't going to be successful either).

Does anybody have any advice? I am using this same implementation against other endpoints of this API, and it is working without issue. The only difference I noticed is that the list values of this response are wrapped in both [ and { brackets, where other endpoints return list values using just the { brackets.

1
  • Try adding the [DataContract] and [DataMember] attribute to you class and members of the class respectively. Commented Jun 9, 2014 at 20:55

1 Answer 1

2

You could try deserializing to a Dictionary<int, Item[]> which should corresponding to the JSON file you have shown in your question:

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;

public class Entry
{
    public string PlayerOrTeamId { get; set; }
    public string PlayerOrTeamName { get; set; }
    public string Division { get; set; }
    public int LeaguePoints { get; set; }
    public int Wins { get; set; }
    public bool IsHotStreak { get; set; }
    public bool IsVeteran { get; set; }
    public bool IsFreshBlood { get; set; }
    public bool IsInactive { get; set; }
}

public class Item
{
    public string Name { get; set; }
    public string Tier { get; set; }
    public string Queue { get; set; }
    public List<Entry> Entries { get; set; }
}

static class Program
{
    static void Main()
    {
        string returnedData =
        @"{
            ""43086"": [{
                ""name"": ""Karthus's Brigands"",
                ""tier"": ""SILVER"",
                ""queue"": ""RANKED_SOLO_5x5"",
                ""entries"": [{
                    ""playerOrTeamId"": ""43086"",
                    ""playerOrTeamName"": ""Testy Test"",
                    ""division"": ""IV"",
                    ""leaguePoints"": 77,
                    ""wins"": 130,
                    ""isHotStreak"": false,
                    ""isVeteran"": true,
                    ""isFreshBlood"": false,
                    ""isInactive"": false
                }]
            }]
        }";
        var leagueStats = JsonConvert.DeserializeObject<Dictionary<int, Item[]>>(returnedData);
        Console.WriteLine(leagueStats.Count);
    }
}

So basically don't trust any automatic tools that are generating crap like invalid_name43086 for you and thinking that they will do the job instead of you. Look at the JSON object you are having and think about how you would represent this JSON structure in C# object. Then the JSON serializer will do the rest for you.

Sign up to request clarification or add additional context in comments.

2 Comments

I actually hand built the object first, based on the raw JSON, to help remind me what actually is going to live in this object. Once my implementation of it didnt work, I remembered that json2csharp exists, and tried that, which gave me the output in my original post. I'm not at the office right now and forgot to sync my source before I left, but I will try your suggestion tomorrow when I get in. Question though, is there a reason to still use DeserializeObject instead of Populate object if you are going to bother to build a dictionary? I was trying to avoid that.
Sorry it took me so long to reply. I finally got around to look at this again and your solution works.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.