Reputation: 31
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.
Upvotes: 3
Views: 98
Reputation: 5161
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
}
Upvotes: 0
Reputation: 42
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);
Upvotes: 2