Reputation: 1
I was learning how to consume API's and got stuck with the error "Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'NbaApi.Models.Standard' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly." Thank you in advance!
I have this json
{
"_internal": {
"pubDateTime": "2020-04-03 13:11:16.692 EDT",
"igorPath": "S3,1585933850069,1585933857347|router,1585933857347,1585933857352|domUpdater,1585933857484,1585933875507|feedProducer,1585933875618,1585933885453",
"xslt": "NBA/xsl/league/roster/marty_active_players.xsl",
"xsltForceRecompile": "true",
"xsltInCache": "false",
"xsltCompileTimeMillis": "253",
"xsltTransformTimeMillis": "7800",
"consolidatedDomKey": "prod__transform__marty_active_players__1989425396229",
"endToEndTimeMillis": "35384"
},
"league": {
"standard": [
{
"firstName": "Steven",
"lastName": "Adams",
"temporaryDisplayName": "Adams, Steven",
"personId": "203500",
"teamId": "1610612760",
"jersey": "12",
"isActive": true,
"pos": "C",
"heightFeet": "6",
"heightInches": "11",
"heightMeters": "2.11",
"weightPounds": "265",
"weightKilograms": "120.2",
"dateOfBirthUTC": "1993-07-20",
"teamSitesOnly": {
"playerCode": "steven_adams",
"posFull": "Center",
"displayAffiliation": "Pittsburgh/New Zealand",
"freeAgentCode": ""
},
"teams": [
{
"teamId": "1610612760",
"seasonStart": "2013",
"seasonEnd": "2019"
}
],
"draft": {
"teamId": "1610612760",
"pickNum": "12",
"roundNum": "1",
"seasonYear": "2013"
},
"nbaDebutYear": "2013",
"yearsPro": "6",
"collegeName": "Pittsburgh",
"lastAffiliation": "Pittsburgh/New Zealand",
"country": "New Zealand"
}
]
}
}
I have these model classes
public class Data
{
public League League { get; set; }
}
public class League
{
public Standard Standard { get; set; }
}
public class Standard
{
public List<Player> players { get; set; }
}
public class Player
{
public string FirstName { get; set; }
public string LastName { get; set; }
//other parameters are also in the model
}
And I use this method to consume the api and pass it to the model
class Program
{
static void Main(string[] args)
{
var data = new List<Data>();
Player player = new Player();
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://data.nba.net/10s/prod/v1/2019/players.json");
//HTTP GET
var responseTask = client.GetAsync("");
responseTask.Wait();
var result = responseTask.Result;
if (result.IsSuccessStatusCode)
{
var readTask = result.Content.ReadAsAsync<List<Data>>();
readTask.Wait();
data = readTask.Result;
foreach (var item in data)
{
var league = item.League;
var standard = league.Standard;
var players = standard.players;
player = players.Where(p => p.LastName == "Adams").First();
}
}
else
{
throw new Exception(result.ReasonPhrase);
}
}
Console.WriteLine(player.LastName);
Console.ReadLine();
}
}
Upvotes: 0
Views: 95
Reputation: 18975
You need change your model as below
public class TeamSitesOnly
{
public string playerCode { get; set; }
public string posFull { get; set; }
public string displayAffiliation { get; set; }
public string freeAgentCode { get; set; }
}
public class Team
{
public string teamId { get; set; }
public string seasonStart { get; set; }
public string seasonEnd { get; set; }
}
public class Draft
{
public string teamId { get; set; }
public string pickNum { get; set; }
public string roundNum { get; set; }
public string seasonYear { get; set; }
}
public class Standard
{
public string firstName { get; set; }
public string lastName { get; set; }
public string temporaryDisplayName { get; set; }
public string personId { get; set; }
public string teamId { get; set; }
public string jersey { get; set; }
public bool isActive { get; set; }
public string pos { get; set; }
public string heightFeet { get; set; }
public string heightInches { get; set; }
public string heightMeters { get; set; }
public string weightPounds { get; set; }
public string weightKilograms { get; set; }
public string dateOfBirthUTC { get; set; }
public TeamSitesOnly teamSitesOnly { get; set; }
public List<Team> teams { get; set; }
public Draft draft { get; set; }
public string nbaDebutYear { get; set; }
public string yearsPro { get; set; }
public string collegeName { get; set; }
public string lastAffiliation { get; set; }
public string country { get; set; }
}
public class League
{
public List<Standard> standard { get; set; }
}
public class Data
{
public League league { get; set; }
}
You can use online tool to convert JSON
object to C#
class structure by http://json2csharp.com/
Upvotes: 0
Reputation: 2166
Change your model (since standard
is an array, not an object):
public class League
{
public List<Player> Standard { get; set; }
}
The response object has structure:
{
"league": {
"standard": [...]
}
}
While your current model tries to deserialize:
{
"league": {
"standard": {
"players": [...]
}
}
}
Upvotes: 0