2

I am trying to use Riot games REST API to make a webapp in C#. I am fine with making the requests using RESTSharp but am having some problems using JSON.Net to convert the returned Json to an object. My request returns a JSON string for example:

{\"dyrus\":{\"id\":4136713,\"name\":\"Dyrus\",\"profileIconId\":23,\"summonerLevel\":1,\"revisionDate\":1376908220000}}

I want to deserialize this into an object that has attributes: id, name, profileIconID, summonerLevel and revisionDate.

The problem I am having is that the information is being deserialized as a string because the Dictionary is nested. What is the best way to just retrieve the nested Dictionary portion of the string: {\"id\":4136713,\"name\":\"Dyrus\",\"profileIconId\":23,\"summonerLevel\":1,\"revisionDate\":1376908220000} and convert it into an object?

Thanks for your help!

Edit:

Here is what I have tried:

public class LeagueUser
{
    public LeagueUser(string json)
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        string jsonString = (string)serializer.DeserializeObject(json);
        LeagueUser test = (LeagueUser)serializer.DeserializeObject(jsonString);
    }
    public int id { get; set; }
    public string name { get; set; }
    public long revisionDate { get; set; }
}
4
  • 1
    What have you tried so far? Did you have a look at the examples here: james.newtonking.com/json/help/index.html Commented Feb 21, 2014 at 15:34
  • Yes I have. I have deserialized it no problem into a string but do not know how to get right to the inner dictionary. Commented Feb 21, 2014 at 15:47
  • 1
    Please edit your question and add the code that you have tried. Commented Feb 21, 2014 at 15:50
  • Is your JSON actually escaped like that (double serialized), or is that just what is showing in the debugger? Commented Feb 21, 2014 at 16:13

2 Answers 2

1

You don't need the constructor, change LeagueUser class to this

public class LeagueUser
{
    public int id { get; set; }
    public string name { get; set; }
    public long revisionDate { get; set; }
}

and use Json.NET to deserialize the json into a Dictionary<string, LeagueUser>

string jsonStr = "{\"dyrus\":{\"id\":4136713,\"name\":\"Dyrus\",\"profileIconId\":23,\"summonerLevel\":1,\"revisionDate\":1376908220000}}";

var deserializedObject = JsonConvert.DeserializeObject<Dictionary<string, LeagueUser>>(jsonStr);

You can get the LeagueUser object this way

LeagueUser leagueUser = deserializedObject["dyrus"];
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much this worked! I know I need to mark this as the answer but don't see where I do that. As soon as I find it I will!
1

You can achieve what you want by creating custom converter for your LeagueUser class:

public class LeagueUserConverter : JsonConverter
{

    public override bool CanConvert(Type objectType)
    {
        return typeof(LeagueUser) == objectType;
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        if (!CanConvert(objectType)) return null;

        var jObject = JObject.Load(reader);

        var user = new LeagueUser
            {
                Id = Convert.ToInt64(jObject["dyrus"]["id"]),
                Name = jObject["dyrus"]["name"].ToString(),
                ProfileIconId = Convert.ToInt32(jObject["dyrus"]["profileIconId"]),
                SummonerLevel = Convert.ToInt32(jObject["dyrus"]["summonerLevel"]),
                RevisionDate = Convert.ToInt64(jObject["dyrus"]["revisionDate"])
            };

        return user;
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

Next you need to decorate your class with the defined converter:

[JsonConverter(typeof(LeagueUserConverter))]
public class LeagueUser
{
    public long Id { get; set; }

    public string Name { get; set; }

    public int ProfileIconId { get; set; }

    public int SummonerLevel { get; set; }

    public long RevisionDate { get; set; }
}

And wherever you need call DeserializeObject method:

var user = JsonConvert.DeserializeObject<LeagueUser>(json);

where the json variable is the json string you posted in your question.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.