1

I'm working on a application that collects data from a Battlelog API in a JSON format. This is what the "player" class looks like in JSONL:

{
   "player":{
      "id":173521680,
      "game":"bf4",
      "plat":"pc",
      "name":"1ApRiL",
      "tag":"",
      "dateCheck":1386842250248,
      "dateUpdate":1386827475171,
      "dateCreate":1383962204725,
      "lastDay":"20131206",
      "country":"DE",
      "countryName":"Germany",
      "rank":{
         "nr":9,
         "imgLarge":"bf4/ranks/r9.png",
         "img":"r9",
         "name":"Lance Corporal IV",
         "needed":140000,
         "next":{
            "nr":10,
            "img":"r10",
            "name":"Lance Corporal V",
            "needed":168000,
            "curr":165309,
            "relNeeded":28000,
            "relCurr":25309,
            "relProg":90.38928571428572
         }
      },
      "score":165309,
      "timePlayed":25740,
      "uId":"2832660339132815718",
      "uName":"1ApRiL",
      "uGava":"7222ff803a0a67404aa082b22ff3fa5b",
      "udCreate":1319474914000,
      "blPlayer":"http://battlelog.battlefield.com/bf4/soldier/1ApRiL/stats/173521680/pc/",
      "blUser":"http://battlelog.battlefield.com/bf4/user/1ApRiL/",
      "editable":false,
      "viewable":true,
      "adminable":false,
      "linked":false
   }
}

This is what my C# class Player looks like:

public class Player
{
    public int id { get; set; }
    public string game { get; set; }
    public string plat { get; set; }
    public string name { get; set; }
    public string tag { get; set; }
    public long dateCheck { get; set; }
    public long dateUpdate { get; set; }
    public long dateCreate { get; set; }
    public string lastDay { get; set; }
    public string country { get; set; }
    public object countryName { get; set; }
    public Rank rank { get; set; }
    public int score { get; set; }
    public int timePlayed { get; set; }
    public string uId { get; set; }
    public string uName { get; set; }
    public string uGava { get; set; }
    public long udCreate { get; set; }
    public string blPlayer { get; set; }
    public string blUser { get; set; }
    public bool editable { get; set; }
    public bool viewable { get; set; }
    public bool adminable { get; set; }
    public bool linked { get; set; }
}

And finally: this is how I try to deserialize it (data being the JSON data):

Player p = JsonConvert.DeserializeObject<Player>(data);

However when I try to run the method it just doesn't return anything, it leaves all the fields blank, even though I get no exceptions or errors whatsoever.

I'm hoping it's something small and simple that I missed, but I just can't figure out what.

2
  • Is that the exact JSON you get from the request? Because it was missing a closing }. I don't think that's the problem, but it's something to check, at least. Commented Dec 12, 2013 at 14:56
  • 1
    @Chris Mantle He said that it's just a paste mistake, and that's the reason I deleted my answer as well :) Commented Dec 12, 2013 at 16:01

1 Answer 1

3

You can use this site to get the correct class definitions

var root  = JsonConvert.DeserializeObject<RootObject>(data);

public class Next
{
    public int nr { get; set; }
    public string img { get; set; }
    public string name { get; set; }
    public int needed { get; set; }
    public int curr { get; set; }
    public int relNeeded { get; set; }
    public int relCurr { get; set; }
    public double relProg { get; set; }
}

public class Rank
{
    public int nr { get; set; }
    public string imgLarge { get; set; }
    public string img { get; set; }
    public string name { get; set; }
    public int needed { get; set; }
    public Next next { get; set; }
}

public class Player
{
    public int id { get; set; }
    public string game { get; set; }
    public string plat { get; set; }
    public string name { get; set; }
    public string tag { get; set; }
    public long dateCheck { get; set; }
    public long dateUpdate { get; set; }
    public long dateCreate { get; set; }
    public string lastDay { get; set; }
    public string country { get; set; }
    public string countryName { get; set; }
    public Rank rank { get; set; }
    public int score { get; set; }
    public int timePlayed { get; set; }
    public string uId { get; set; }
    public string uName { get; set; }
    public string uGava { get; set; }
    public long udCreate { get; set; }
    public string blPlayer { get; set; }
    public string blUser { get; set; }
    public bool editable { get; set; }
    public bool viewable { get; set; }
    public bool adminable { get; set; }
    public bool linked { get; set; }
}

public class RootObject
{
    public Player player { get; set; }
}
Sign up to request clarification or add additional context in comments.

3 Comments

That's exactly the website i used to create the class definitions before, so i don't think that could be it.
@user2722107 you deserialize to Player object. See the answer, I used RootObject
Sorry, i missed that, it worked!! Thank you very much!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.