1

I'm currently working on a project where I make a request to the Riot Games API, parse the JSON, and do some stuff with it. I have the request working, and I know I'm getting valid JSON. My issue is using JSON.Net to deserialize the JSON.

The JSON is of the following structure:

{
  "xarcies": {
        "id": 31933985,
        "name": "Farces",
        "profileIconId": 588,
        "revisionDate": 1450249383000,
        "summonerLevel": 30
    }
}

I want to load this data into the following class

[JsonObject(MemberSerialization.OptIn)]
class Summoner
{
    [JsonProperty("id")]
    public long id {get;set;}

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

    [JsonProperty("profileIconId")]
    public int profileIconId { get; set; }

    [JsonProperty("revisionDate")]
    public long revisionDate { get; set; }

    [JsonProperty("summonerLevel")]
    public long summonerLevel { get; set; }
}

The issue I'm having is that because I'm given a "xarcies" object that contains the information I need, I'm not sure how to go about designing a class that can accept the JSON data. I've seen some examples that use a RootObject class to take the object and that class has a subclass that all the pairs are put into, but I can't seem to get it to work. Every time I run it the attributes for the object end up being NULL.

Jatrammel
  • 13
  • 7

2 Answers2

1

You can deserialize your JSON as a Dictionary<string, Summoner>:

var root = JsonConvert.DeserializeObject<Dictionary<string, Summoner>>(jsonString);

The dictionary will be keyed by the user name, in this case "xarcies". See Deserialize a Dictionary.

dbc
  • 104,963
  • 20
  • 228
  • 340
  • Are you the Redditor that pointed me in this direction as well? Regardless, this is definitely the answer I was looking for. – Jatrammel Dec 16 '15 at 22:30
0

I just used json2csharp to create the following class (its types look a bit different then yours):

public class UserData
{
    public int id { get; set; }
    public string name { get; set; }
    public int profileIconId { get; set; }
    public long revisionDate { get; set; }
    public int summonerLevel { get; set; }
}

public class RootObject
{
    public KeyValuePair<string, UserData> value { get; set; }
}
cramopy
  • 3,459
  • 6
  • 28
  • 42
  • Okay. That will help with the structure of the classes themselves. Also did not know about json2csharp. Nifty tool. However, I still have a lingering question. This works for this specific object. However, xarcies is a user. That means that depending on which user I request from the API, the class structure would change. Is there a generic way to go about this? – Jatrammel Dec 16 '15 at 22:12
  • @Jatrammel can you please try the above? – cramopy Dec 16 '15 at 22:18
  • Just tried it. `value.Key` and `value.Value` both come back as `null`. – dbc Dec 16 '15 at 22:22
  • @dbc sorry, just read it on a dirfferent question... maybe `Summoner` works better (not better - it works). but I can't copy-paste that from you, can't I?.... – cramopy Dec 16 '15 at 22:23
  • Json.NET doesn't serialize a single key/value pair as a dictionary, it serializes it like so: `{"value":{"Key":"xarcies","Value":{"id":31933985,"name":"Farces","profileIconId":588,"revisionDate":1450249383000,"summonerLevel":30}}}`. – dbc Dec 16 '15 at 22:25
  • So, I've been using multiple sources to troubleshoot the this problem. Someone actually suggested I just deserialize into a dictionary that has a string and summoner for a key-value pair, like so var summonerContainer = JsonConvert.DeserializeObject>(json); – Jatrammel Dec 16 '15 at 22:26
  • @Jatrammel got mine from http://stackoverflow.com/a/27327003/4000926 as I didn't knew that... – cramopy Dec 16 '15 at 22:28
  • Hmm. [that answer](http://stackoverflow.com/questions/27326960/json-serialization-variable-propertyname/27327003#27327003) looks wrong. – dbc Dec 16 '15 at 22:42