1

I am pulling some JSON from an API. It comes in this format:

{"yourname": {
"id": 42728521,
"name": "Your Name",
"profileIconId": 27,
"revisionDate": 1397930999000,
"summonerLevel": 1
}}

However, that is stored as:

"{\"yourname\":{\"id\":42728521,\"name\":\"Your Name\",\"profileIconId\":27,\"summonerLevel\":1,\"revisionDate\":1397930999000}}"

Inside the string.

I want to be able to call summonerLevel and for it to return the correct value.

I've been trying this:

using (var client = new WebClient())
        {
            api_return = client.DownloadString(api_call_key);
        }

 var foo = JsonConvert.DeserializeObject<Summoner>(api_return);

        Console.WriteLine(Summoner.id);
        Console.WriteLine(Summoner.name);
        Console.WriteLine(Summoner.summonerLevel);

        string id_ = foo.ToString();

        Console.WriteLine(id_);

        Console.ReadKey();

    }

    public class Summoner
    {
        public static int id { get; set; }
        public static string name { get; set; }
        public static int summonerLevel { get; set; }
    }

However, that just prints out:

0 nothing 0

Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
Logan W
  • 45
  • 2
  • 5
  • Take a look at [this](http://stackoverflow.com/questions/6620165/how-to-parse-json-in-c) – LJᛃ Oct 10 '14 at 02:04
  • You're storing the deserialized object inside your `foo` variable, use it to access the values. – LJᛃ Oct 10 '14 at 02:05
  • Apparantly I'm not storing them in the foo variable http://i.gyazo.com/28b9d4c00455784bfcd066f522397015.png – Logan W Oct 10 '14 at 02:12

2 Answers2

2

You have two issues here, which are both preventing you from getting the data from the JSON.

The first issue is that the properties of your Summoner class should not be static, as Vikas pointed out. Define your class like this:

public class Summoner
{
    public int id { get; set; }
    public string name { get; set; }
    public int summonerLevel { get; set; }
}

The second issue is that your JSON structure doesn't match what you're deserializing into. The id, name and summonerLevel properties are not at the root level of the JSON, they are one level further down, inside another object. So, you'll need to deserialize into some class that "wraps" your Summoner. If the yourname property in the JSON were a fixed value, you could define a Wrapper class like this to deserialize into:

public class Wrapper
{
    [JsonProperty("yourname")]
    public Summoner Summoner { get; set; }
}

However, since the yourname property in the JSON is likely not a fixed value (it could change for different summoners), I would recommend deserializing into a Dictionary<string, Summoner> like this instead:

var dict = JsonConvert.DeserializeObject<Dictionary<string, Summoner>>(json);

From there, you can either loop through the dictionary key-value pairs, or, if you're only expecting one, you can use First() to get it.

var summoner = dict.First().Value;

Here is a full demo:

string json = @"{""yourname"": {
                ""id"": 42728521,
                ""name"": ""Your Name"",
                ""profileIconId"": 27,
                ""revisionDate"": 1397930999000,
                ""summonerLevel"": 1
                }}";

var dict = JsonConvert.DeserializeObject<Dictionary<string, Summoner>>(json);
var summoner = dict.First().Value;

Console.WriteLine(summoner.id);
Console.WriteLine(summoner.name);
Console.WriteLine(summoner.summonerLevel);

Output:

42728521
Your Name
1
Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
1

Properties in Summoner class should not be static

using (var client = new WebClient())
{
    api_return = client.DownloadString(api_call_key);
}

var foo = JsonConvert.DeserializeObject<Summoner>(api_return);

Console.WriteLine(foo.id);
Console.WriteLine(foo.name);
Console.WriteLine(foo.summonerLevel);

..............

}

public class Summoner
{
    public int id { get; set; }
    public string name { get; set; }
    public int summonerLevel { get; set; }
}
Vikas Gupta
  • 4,455
  • 1
  • 20
  • 40