3

I'm reletively new to C# (previous experience with HTML/JS/Angular) and I am having issues with deserializing the JSON i'm reciving back from an API I am using.

{  
   "titles":[  
  {  
     "lastUnlock":"2016-12-28T16:34:36.0390000Z",
     "titleId":566278,
     "serviceConfigId":"6ee10100-671e-4fc4-8cf1-91700008a406",
     "titleType":"DGame",
     "platform":"Durango",
     "name":"Game1",
     "earnedAchievements":4,
     "currentGamerscore":60,
     "maxGamerscore":1000
  },
  {  
     "lastUnlock":"2016-08-05T13:02:18.4140000Z",
     "titleId":10027721,
     "serviceConfigId":"28dd0100-1521-414e-a1d8-f0ba009902c9",
     "titleType":"DGame",
     "platform":"Durango",
     "name":"Game2",
     "earnedAchievements":17,
     "currentGamerscore":1000,
     "maxGamerscore":1000
  },
  {  
     "lastUnlock":"2016-05-02T20:52:40.3705214Z",
     "titleId":62572131,
     "serviceConfigId":"54240100-7870-4a47-8cec-7cfd03bac663",
     "titleType":"DGame",
     "platform":"Durango",
     "name":"Game3",
     "earnedAchievements":35,
     "currentGamerscore":1000,
     "maxGamerscore":1000
  },
     ],
   "pagingInfo":{  
      "continuationToken":null,
      "totalRecords":86
   }
}

The issue is I am not sure how to deserialize this in to an array of objects.

I have created an object class:

public class Game
    {
        public string name { get; set; }
        public string gamertag { get; set; }
        public string platform { get; set; }
        public int earnedAchievements { get; set; }
        public string currentGamerscore { get; set; }
        public string maxGamerscore { get; set; }
        public string lastUnlock { get; set; }
    }

From there i've tried using JsonConvert.DeserializeObject(result) but this just returns "CompleteAdmin.Controllers.AchievementsAPIController+Game" which isn't usable.

Can anybody show me how this is supposed to be setup? Ultimately i'm aiming to get this in to a DB. :)

Thanks.

4
  • TThis is how to do it with Json.NET newtonsoft.com/json/help/html/DeserializeObject.htm Commented Aug 26, 2017 at 13:57
  • As far as I can see that's pretty much my current setup: games = JsonConvert.DeserializeObject<Game>(result); Commented Aug 26, 2017 at 14:08
  • If your JSON is a collection then it would be JsonConvert.DeserializeObject<List<Game>> or whatever collection it is. Commented Aug 26, 2017 at 14:09
  • Just a heads up, when trying to deserialize your JSON I got an error message about a trailing comma that shouldn't be there. It's the second one counting back from pagingInfo. Commented Aug 26, 2017 at 14:26

2 Answers 2

2

Its simple like

in visual studio right click on the solution and choose "Manage NuGet Packages" a menu will be open in the top search type "newtonsoft" select the very first option with black icon. and add to your project. then write the following.

public class Games
{
    public Game[] titles { get; set; }
}

public class Game
{


    public string name { get; set; }
    public string gamertag { get; set; }
    public string platform { get; set; }
    public int earnedAchievements { get; set; }
    public string currentGamerscore { get; set; }
    public string maxGamerscore { get; set; }
    public string lastUnlock { get; set; }
}

On page load or where you want the result :

 string jsonObject = @"{  
                                   'titles':[  
                                  {  
                                     'lastUnlock':'2016-12-28T16:34:36.0390000Z',
                                     'titleId':566278,
                                     'serviceConfigId':'6ee10100-671e-4fc4-8cf1-91700008a406',
                                     'titleType':'DGame',
                                     'platform':'Durango',
                                     'name':'Game1',
                                     'earnedAchievements':4,
                                     'currentGamerscore':60,
                                     'maxGamerscore':1000
                                  },
                                  {  
                                     'lastUnlock':'2016-08-05T13:02:18.4140000Z',
                                     'titleId':10027721,
                                     'serviceConfigId':'28dd0100-1521-414e-a1d8-f0ba009902c9',
                                     'titleType':'DGame',
                                     'platform':'Durango',
                                     'name':'Game2',
                                     'earnedAchievements':17,
                                     'currentGamerscore':1000,
                                     'maxGamerscore':1000
                                  },
                                  {  
                                     'lastUnlock':'2016-05-02T20:52:40.3705214Z',
                                     'titleId':62572131,
                                     'serviceConfigId':'54240100-7870-4a47-8cec-7cfd03bac663',
                                     'titleType':'DGame',
                                     'platform':'Durango',
                                     'name':'Game3',
                                     'earnedAchievements':35,
                                     'currentGamerscore':1000,
                                     'maxGamerscore':1000
                                  },
                                     ],
                                   'pagingInfo':{  
                                      'continuationToken':null,
                                      'totalRecords':86
                                   }
                                }";


        var games = JsonConvert.DeserializeObject<Games>(jsonObject);
Sign up to request clarification or add additional context in comments.

Comments

0

Just add an extra class to contain your collection of Titles (or Games, better make up your mind...)

public class Container
{
    public Game[] Titles { get; set; }
}

Now you can easily deserialize like this:

var res = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<Container>(jsonObject);

To use this, add a reference to System.Web.Extensions to your project.

Note that I had to remove a comma from your JSON to get this to work:

 },    <------ this comma should not be there
  ],
"pagingInfo":{  
   "continuationToken":null,
   "totalRecords":86
}

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.