Joris Mathijssen
Joris Mathijssen

Reputation: 669

Json and a .NET class which will match JSON data format?

I'm working on a small project in C# using Json. I have some troubles understanding Json and C#, i think i got my Json Data format wrong but im not sure.'

Let me show you:

{
"response": {
    "success": 1,
    "current_time": 1388791039,
    "players": {
        "0": {
            "steamid": "4235647457865",
            "success": 1,
            "backpack_value": 1,
            "backpack_update": 1,
            "name": "Test",
            "notifications": 0
        }
    }
}
  • Json Format i recieve.
    public class Player
{
    public string steamid { get; set; }
    public int success { get; set; }
    public double backpack_value { get; set; }
    public int backpack_update { get; set; }
    public string name { get; set; }
    public int stats_tf_reputation { get; set; }
    public int stats_tf_supporter { get; set; }
    public bool steamrep_scammer { get; set; }
    public bool ban_economy { get; set; }
}

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

public class JsonData
{
    public Response response { get; set; }
}
  • The data format class i created.

I guess it goes horribly wrong with the ""0": {" part. I tried a few different ways but i cant fix it.

I hope someone can help me out!

Upvotes: 1

Views: 1011

Answers (2)

STLDev
STLDev

Reputation: 6174

It looks okay to me, except that the Player class you've listed doesn't match the JSON data. Notice that the data has a member named notifications which is an integer, yet your Player class does not have that.

Upvotes: 0

StriplingWarrior
StriplingWarrior

Reputation: 156634

Instead of a List<Player>, you could use a Dictionary<int, Player>. That would allow you to handle the {"0": {...}} format, by keying each Player object based on the number value associated with it.

Upvotes: 1

Related Questions