2

I have tried countless methods to Parse my JSON string (Steam Public Data), yet nothing seems to work. I just want to be able to extract values from the string. For Example, obtaining the value of personaname which would return SlothGod. I have JSON.NET installed in my project.

Here is my JSON:

{
    "response": {
        "players": [
            {
                "steamid": "76561198301407459",
                "communityvisibilitystate": 3,
                "profilestate": 1,
                "personaname": "SlothGod",
                "lastlogoff": 1508389707,
                "commentpermission": 1,
                "profileurl": "http://steamcommunity.com/id/sleuthgud/",
                "avatar": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/09/09cea52b91136fb3306c57771a746db2823b91ba.jpg",
                "avatarmedium": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/09/09cea52b91136fb3306c57771a746db2823b91ba_medium.jpg",
                "avatarfull": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/09/09cea52b91136fb3306c57771a746db2823b91ba_full.jpg",
                "personastate": 0,
                "realname": "Josh",
                "primaryclanid": "103582791460168790",
                "timecreated": 1462086929,
                "personastateflags": 0,
                "loccountrycode": "AU",
                "locstatecode": "QLD"
            }
        ]

    }
}

Main method suggested to me:

public class Details
{
    public string personaname { get; set; }
}
private void GetSteamDetails()
{
    var data = Newtonsoft.Json.JsonConvert.DeserializeObject<Details>(SteamDetailsJson);
    SteamName = data.personaname;
}

This is placed before Page_Load(). I then call GetSteamDetails(); when I want to fetch the name.

SlothGod
  • 356
  • 1
  • 4
  • 19
  • You should show what you have tried.. and we can try and help fix it – Gilad Green Oct 19 '17 at 06:33
  • 3
    Possible duplicate of [How can I parse JSON with C#?](https://stackoverflow.com/questions/6620165/how-can-i-parse-json-with-c) –  Oct 19 '17 at 06:34
  • @TAHASULTANTEMURI I have looked at that exact post myself, yet I was never able to get it working – SlothGod Oct 19 '17 at 06:38
  • not clear question. provide what you tried so far and where exactly you are facing problem. – Md. Tazbir Ur Rahman Bhuiyan Oct 19 '17 at 06:42
  • OP should first study how to parse JSON in c#. The complexity level of JSON parsing is really low specially with the right tools and valid format. – jegtugado Oct 19 '17 at 07:18
  • @JohnEphraimTugado Trust me, I have been studying it, but I just can't seem to understand it, and the easiest way to learn something is by having an example show to you that you actually understand. – SlothGod Oct 19 '17 at 07:27

4 Answers4

9

After my question being down voted, I decided to not give up on this problem. After extensive research, trial and error, and YouTube tutorials which are the most helpful IMO. I found that the data was containing a JSON array, and yes I will admit, I was confused with this, but the answer was to simply treat it like a C# array and add [1] to the end of players.

Details details = new Details();
public class Details
{
    public string avatar { get; set; }
    public string avatarmedium { get; set; }
    public string avatarfull { get; set; }
    public string realname { get; set; }
    public string personaname { get; set; }
    public string steamid { get; set; }
}

private void GetSteamDetails()
{
    var SteamDetails= JsonConvert.DeserializeObject<dynamic>(SteamDetailsJson);
    avatar = SteamDetails.response.players[1].avatar.ToString();
    personaname = SteamDetails.response.players[1].personaname.ToString();
}
SlothGod
  • 356
  • 1
  • 4
  • 19
4

Based on the JSON string you provided, you should have the following C# classes to support it, or to deserialize the JSON object values into: I used this link to generate the classes.

public class Player
{
    public string steamid { get; set; }
    public int communityvisibilitystate { get; set; }
    public int profilestate { get; set; }
    public string personaname { get; set; }
    public int lastlogoff { get; set; }
    public int commentpermission { get; set; }
    public string profileurl { get; set; }
    public string avatar { get; set; }
    public string avatarmedium { get; set; }
    public string avatarfull { get; set; }
    public int personastate { get; set; }
    public string realname { get; set; }
    public string primaryclanid { get; set; }
    public int timecreated { get; set; }
    public int personastateflags { get; set; }
    public string loccountrycode { get; set; }
    public string locstatecode { get; set; }
}

public class Response
{
    public List<Player> players { get; set; }
}

public class RootObject
{
    public Response response { get; set; }
}

Then, using Newtonsoft.Json, you can deserialize the JSON object into your C# classes as follow:

JsonConvert.DeserializeObject<RootObject>("yourJsonString");
monstertjie_za
  • 7,277
  • 8
  • 42
  • 73
2

You mention that Newtonsoft.Json already referenced in the project.

Use class to represent json data structure, then you can easy deserialize it.
You can use only properties you need in the class.

public class Player
{
    public string personaname { get; set; } 
}

var player = Newtonsoft.Json.JsonConvert.DeserializeObject<Player>(jsonString);

// use player.personaname

For updates question create classes which represent your data structure

public class Team
{
    public List<Player> players { get; set; } 
}

public class Response
{
    public Team response { get; set; } 
}
Fabio
  • 31,528
  • 4
  • 33
  • 72
  • I have been very confused about the DeserializeObject method, would you be able to explain what I add in place of `Data`? – SlothGod Oct 19 '17 at 06:37
  • Create class `Data` or name it how you want. – Fabio Oct 19 '17 at 06:38
  • I edited my question and added the changes that I made. SteamName is still returning null. – SlothGod Oct 19 '17 at 07:00
  • No very nice to edit questions this way, but simply you need to create classes which represents your data structure - you can use some online converters from json to c# classes - just search for "json to c# class" – Fabio Oct 19 '17 at 07:05
2

You can use http://json2csharp.com/ to generate a class automatically from a JSON string.

jegtugado
  • 5,081
  • 1
  • 12
  • 35
jmunoa7
  • 185
  • 2
  • 15
  • I dont know why people down voted this answer ? This is a good one. –  Oct 19 '17 at 07:05
  • I am not downvoter, but I think because "link" answers not very welcome on SO – Fabio Oct 19 '17 at 07:07
  • @Fabio I suspect that's why. That page goes down and this answer is useless. It's better as an aside to an existing answer. – ProgrammingLlama Oct 19 '17 at 07:15
  • I did downvote that answer because the OP is trying to convert in runtime. The question is not "How to write a C# class based on a JSON string". I bet he can write his classes by himself. – VTodorov Oct 19 '17 at 07:27