0

I'm currently using an API to grab some player information, which looks like this in raw data form:

{
  "response": {
    "players": [
      {
        "steamid": "76561198166559342",
        "communityvisibilitystate": 1,
        "profilestate": 1,
        "personaname": "Stack Overflow",
        "commentpermission": 2,
        "profileurl": "https://steamcommunity.com/id/stackoverflow/",
        "avatar": "https://avatars.akamai.steamstatic.com/8a2e52a3eaefed0296459fa811aasdasd1ce29374.jpg",
        "avatarmedium": "https://avatars.akamai.steamstatic.com/8a2e52a3eaefed0296459fa81asdas1ce29374_medium.jpg",
        "avatarfull": "https://avatars.akamai.steamstatic.com/8a2e52a3eaefed0296459fa811a61212312e29374_full.jpg",
        "avatarhash": "8a2e52a3eaefed02asdasd",
        "personastate": 0
      }
    ]
  }
}

I am trying to just grab the "personaname" and "avatar" values from my data, which have been quite unsuccessful, so I've tried to objectify the JSON before getting it, which still isn't working:

Models.SteamUser persona;

String httpres;
using (var client = new HttpClient())
{
    string steamurl = "MY HIDDEN URL HERE" + s64;
    var steamapi = new Uri(steamurl);
    var result = client.GetAsync(steamapi).Result.Content.ReadAsStringAsync().Result;
    var json = result.ToString();
    httpres = json;
    var jUser = JObject.Parse(json);
    var userData = JsonConvert.DeserializeObject<Models.SteamUser>(jUser["players"]).ToString(); ;
    persona = userData;
}

Is there something I'm missing?

Yong Shun
  • 35,286
  • 4
  • 24
  • 46
  • 1. To get the players, `jUser["response"]["players"]` and deserialize as `List`. `jUser["response"]["players"]` is an array. To get first item, you need `.First()` or `.FirstOrDefault()`. 2. You are assigned `userData` which is a `string` to persona (`Models.SteamUser`). Doubt that it will not be working as both are different types. – Yong Shun Jun 09 '22 at 03:34
  • 1
    3. It's not suggested to perform `Task.Result` as it makes to call the thread synchronously and may lead to a deadlock. Reading: [What happens while waiting on a Task's Result?](https://stackoverflow.com/questions/12484112/what-happens-while-waiting-on-a-tasks-result) – Yong Shun Jun 09 '22 at 03:38

1 Answers1

1
  1. To get players from the JSON response, you need jObj["response"]["players"] instead of jObj["players"].

  2. You have to deserialize as List<Models.SteamUser> but not Models.SteamUser as it is an array. Then with IEnumerable.FirstOrDefault() to get the first item of the array.

  3. From the existing code, the userData is string type while persona is List<Models.SteamUser> type. You can't assign userData to persona.

  4. Not suggested using Task.Result as it performs the operation synchronously and waits for the operation to be completed. Hence, it may lead to a deadlock. Instead, works with async/await. Source: What happens while waiting on a Task's Result?

Your code should be looked as below:

Models.SteamUser persona = null;

using (var client = new HttpClient())
{
    string steamurl = "MY HIDDEN URL HERE" + s64;
    var steamapi = new Uri(steamurl);
    var response = await client.GetAsync(steamapi);

    var jsonResult = await response.Content.ReadAsStringAsync();

    var jObj = JObject.Parse(jsonResult);
    var players = JsonConvert.DeserializeObject<List<Models.SteamUser>>(jObj["response"]["players"].ToString());

    // Or
    // var players = (jObj["response"]["players"] as JArray).ToObject<List<Models.SteamUser>>();
    
    persona = players.FirstOrDefault();
}
Yong Shun
  • 35,286
  • 4
  • 24
  • 46