2

I am developing an application in c# that gets the players backpack value on a game called TF2 using Backpack.tf's api.

At the moment the code is:

    (MAIN CLASS)
    JsonConvert.DeserializeObject<Json1>(json);
    (END OF MAIN CLASS)

public class Json1 {
    public static List<Json2> response { get; set; }
}
public class Json2
{
    public static int success { get; set; }
    public static int current_time { get; set; }
    public static IEnumerable<Json4> players { get; set; }
}
public class Json4 {
    public static int steamid { get; set; }
    public static int success { get; set; }
    public static double backpack_value { get; set; }
    public static string name { get; set; }
}

I've cut out all the other crap out of the main class etc but suffice it to say that yes i've got the json code into the json string ready for deserializing (Tested it with Console.Writeline)

The problem is. Whenever I use things like Json4.name (when writing to console) it always returns 0.

Sorry if I've made a stupid mistake but I think I've tried things like removing static, changing variable types etc but I still can't get it to work. Please note this is my first attemopt at deserializing Json code and I wrote the classes at bottom myself because some reason http://json2csharp.com/ didn't work. Heres the Json I am trying to deserialize:

{
   "response":{
      "success":1,
      "current_time":1365261392,
      "players":{
         "0":{
            "steamid":"76561198045802942",
            "success":1,
            "backpack_value":12893.93,
            "backpack_update":1365261284,
            "name":"Brad Pitt",
            "stats_tf_reputation":2257,
            "stats_tf_supporter":1,
            "notifications":0
         },
         "1":{
            "steamid":"76561197960435530",
            "success":1,
            "backpack_value":4544.56,
            "backpack_update":1365254794,
            "name":"Robin",
            "notifications":0
         }
      }
   }
}

(formatting messed up a bit. Also please excuse some spelling mistakes :) )

1
  • 2
    All your fields are static. Those really should be non-static. Commented Apr 6, 2013 at 16:13

1 Answer 1

4

You have several problems with your code:

a) All your fields are static. Remove static; you need them to be instance members.

b) The response property in Json1 should be just a single instance, not a list.

c) Players needs to be a dictionary (or custom type), not an IEnumerable, since it is not an array in the JSON.

d) StreamId has really big numbers that will not fit into an int; change this to long (or string).

public class Json1
{
    public Json2 response { get; set; }
}

public class Json2
{
    public int success { get; set; }
    public int current_time { get; set; }
    public IDictionary<int, Json4> players { get; set; }
}

public class Json4
{
    public long steamid { get; set; }
    public int success { get; set; }
    public double backpack_value { get; set; }
    public string name { get; set; }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for this but I have never used the IDictionary type before. How would I output the information inside of it? Thanks for the response again though.
IDictionary is IEnumerable<KeyValuePair<int, Json4>>, so you can just use the same loop, like you had (foreach I guess), and access indexes (int) by a.Key and access values (Json4) by a.Value.
@MrShaunh77 please mark this answer as a solution then, so this thread will be marked as solved.

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.