1

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!

2
  • your post does not say what you are trying to do nor what goes wrong. are you trying to serialize the objects to look like the JSON above? are you trying to deserialize the JSON above into those c# objects? we dont know what your goal is... Commented Jan 4, 2014 at 0:34
  • Why are you using a json object with integer property names rather than a json array? Commented Jan 4, 2014 at 0:40

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.