Home > Enterprise >  Display Consumed API on web Page
Display Consumed API on web Page

Time:08-22

So ive connected a n external API and I want to display it on the page, but im not getting any luck.

I'm using RestSharp so I cant just collect the infromation as such:

string results = response.Content.ReadAsStringAsync().Result;

Also because i'm using RestSharp, collecting the returned api information with:

string results = response.Content;

this causes the program to crash because it cannot be inserted into the data table. From what im seeing it needs to be deserialized, but im not sure how thats done in RestSharp.

EDIT 1: This is my model class based on the API results

using Newtonsoft.Json;

namespace NBA_API_TEST.Models { public class NBAModel { [JsonProperty("id")] public long Id { get; set; }

    [JsonProperty("league")]
    public string League { get; set; }

    [JsonProperty("season")]
    public long Season { get; set; }

    [JsonProperty("date")]
    public Date Date { get; set; }

    [JsonProperty("stage")]
    public long Stage { get; set; }

    [JsonProperty("status")]
    public Status Status { get; set; }

    [JsonProperty("periods")]
    public Periods Periods { get; set; }

    [JsonProperty("arena")]
    public Arena Arena { get; set; }

    [JsonProperty("teams")]
    public Teams Teams { get; set; }

    [JsonProperty("scores")]
    public Scores Scores { get; set; }

    [JsonProperty("officials")]
    public object[] Officials { get; set; }

    [JsonProperty("timesTied")]
    public object TimesTied { get; set; }

    [JsonProperty("leadChanges")]
    public object LeadChanges { get; set; }

    [JsonProperty("nugget")]
    public object Nugget { get; set; }
}

public partial class Arena
{
    [JsonProperty("name")]
    public object Name { get; set; }

    [JsonProperty("city")]
    public object City { get; set; }

    [JsonProperty("state")]
    public object State { get; set; }

    [JsonProperty("country")]
    public object Country { get; set; }
}

public partial class Date
{
    [JsonProperty("start")]
    public DateTimeOffset Start { get; set; }

    [JsonProperty("end")]
    public object End { get; set; }

    [JsonProperty("duration")]
    public object Duration { get; set; }
}

public partial class Periods
{
    [JsonProperty("current")]
    public long Current { get; set; }

    [JsonProperty("total")]
    public long Total { get; set; }

    [JsonProperty("endOfPeriod")]
    public object EndOfPeriod { get; set; }
}

public partial class Scores
{
    [JsonProperty("visitors")]
    public ScoresHome Visitors { get; set; }

    [JsonProperty("home")]
    public ScoresHome Home { get; set; }
}

public partial class ScoresHome
{
    [JsonProperty("win")]
    public object Win { get; set; }

    [JsonProperty("loss")]
    public object Loss { get; set; }

    [JsonProperty("series")]
    public Series Series { get; set; }

    [JsonProperty("linescore")]
    public object[] Linescore { get; set; }

    [JsonProperty("points")]
    public long Points { get; set; }
}

public partial class Series
{
    [JsonProperty("win")]
    public object Win { get; set; }

    [JsonProperty("loss")]
    public object Loss { get; set; }
}

public partial class Status
{
    [JsonProperty("clock")]
    public object Clock { get; set; }

    [JsonProperty("halftime")]
    public object Halftime { get; set; }

    [JsonProperty("short")]
    public long Short { get; set; }

    [JsonProperty("long")]
    public string Long { get; set; }
}

public partial class Teams
{
    [JsonProperty("visitors")]
    public TeamsHome Visitors { get; set; }

    [JsonProperty("home")]
    public TeamsHome Home { get; set; }
}

public partial class TeamsHome
{
    [JsonProperty("id")]
    public long Id { get; set; }

    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("nickname")]
    public string Nickname { get; set; }

    [JsonProperty("code")]
    public string Code { get; set; }

    [JsonProperty("logo")]
    public Uri Logo { get; set; }
}

}

from the API call 5 results should be returned. So I have these set up

 IList<NBAModel> GameModel = new List<NBAModel>();

GameModel = JsonConvert.DeserializeObject<List<NBAModel>>(results);

But that causes this error to be returned:

JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[NBA_API_TEST.Models.NBAModel]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.

EDIT 2 Update with my JSON

{
"get": "games/",
"parameters": {
    "season": "2021",
    "h2h": "1-2"
},
"errors": [],
"results": 5,
"response": [
    {
        "id": 9484,
        "league": "vegas",
        "season": 2021,
        "date": {
            "start": "2021-08-08T20:00:00.000Z",
            "end": null,
            "duration": null
        },
        "stage": 2,
        "status": {
            "clock": null,
            "halftime": null,
            "short": 3,
            "long": "Finished"
        },
        "periods": {
            "current": 4,
            "total": 4,
            "endOfPeriod": null
        },
        "arena": {
            "name": null,
            "city": null,
            "state": null,
            "country": null
        },
        "teams": {
            "visitors": {
                "id": 2,
                "name": "Boston Celtics",
                "nickname": "Celtics",
                "code": "BOS",
                "logo": "https://upload.wikimedia.org/wikipedia/fr/thumb/6/65/Celtics_de_Boston_logo.svg/1024px-Celtics_de_Boston_logo.svg.png"
            },
            "home": {
                "id": 1,
                "name": "Atlanta Hawks",
                "nickname": "Hawks",
                "code": "ATL",
                "logo": "https://upload.wikimedia.org/wikipedia/fr/e/ee/Hawks_2016.png"
            }
        },
        "scores": {
            "visitors": {
                "win": null,
                "loss": null,
                "series": {
                    "win": null,
                    "loss": null
                },
                "linescore": [],
                "points": 85
            },
            "home": {
                "win": null,
                "loss": null,
                "series": {
                    "win": null,
                    "loss": null
                },
                "linescore": [],
                "points": 83
            }
        },
        "officials": [],
        "timesTied": null,
        "leadChanges": null,
        "nugget": null
    },
    {
        "id": 9780,
        "league": "standard",
        "season": 2021,
        "date": {
            "start": "2021-11-18T00:30:00.000Z",
            "end": "2021-11-18T02:43:00.000Z",
            "duration": "2:03"
        },
        "stage": 2,
        "status": {
            "clock": null,
            "halftime": false,
            "short": 3,
            "long": "Finished"
        },
        "periods": {
            "current": 4,
            "total": 4,
            "endOfPeriod": false
        },
        "arena": {
            "name": "State Farm Arena",
            "city": "Atlanta",
            "state": "GA",
            "country": "USA"
        },
        "teams": {
            "visitors": {
                "id": 2,
                "name": "Boston Celtics",
                "nickname": "Celtics",
                "code": "BOS",
                "logo": "https://upload.wikimedia.org/wikipedia/fr/thumb/6/65/Celtics_de_Boston_logo.svg/1024px-Celtics_de_Boston_logo.svg.png"
            },
            "home": {
                "id": 1,
                "name": "Atlanta Hawks",
                "nickname": "Hawks",
                "code": "ATL",
                "logo": "https://upload.wikimedia.org/wikipedia/fr/e/ee/Hawks_2016.png"
            }
        },
        "scores": {
            "visitors": {
                "win": 7,
                "loss": 8,
                "series": {
                    "win": 0,
                    "loss": 1
                },
                "linescore": [
                    "29",
                    "20",
                    "28",
                    "22"
                ],
                "points": 99
            },
            "home": {
                "win": 7,
                "loss": 9,
                "series": {
                    "win": 1,
                    "loss": 0
                },
                "linescore": [
                    "30",
                    "28",
                    "33",
                    "19"
                ],
                "points": 110
            }
        },
        "officials": [
            "Leon Wood",
            "Zach Zarba",
            "Suyash Mehta"
        ],
        "timesTied": null,
        "leadChanges": null,
        "nugget": null
    },
    {
        "id": 10302,
        "league": "standard",
        "season": 2021,
        "date": {
            "start": "2022-01-29T00:30:00.000Z",
            "end": "2022-01-29T02:54:00.000Z",
            "duration": "2:13"
        },
        "stage": 2,
        "status": {
            "clock": null,
            "halftime": false,
            "short": 3,
            "long": "Finished"
        },
        "periods": {
            "current": 4,
            "total": 4,
            "endOfPeriod": false
        },
        "arena": {
            "name": "State Farm Arena",
            "city": "Atlanta",
            "state": "GA",
            "country": "USA"
        },
        "teams": {
            "visitors": {
                "id": 2,
                "name": "Boston Celtics",
                "nickname": "Celtics",
                "code": "BOS",
                "logo": "https://upload.wikimedia.org/wikipedia/fr/thumb/6/65/Celtics_de_Boston_logo.svg/1024px-Celtics_de_Boston_logo.svg.png"
            },
            "home": {
                "id": 1,
                "name": "Atlanta Hawks",
                "nickname": "Hawks",
                "code": "ATL",
                "logo": "https://upload.wikimedia.org/wikipedia/fr/e/ee/Hawks_2016.png"
            }
        },
        "scores": {
            "visitors": {
                "win": 25,
                "loss": 25,
                "series": {
                    "win": 0,
                    "loss": 2
                },
                "linescore": [
                    "20",
                    "27",
                    "30",
                    "15"
                ],
                "points": 92
            },
            "home": {
                "win": 23,
                "loss": 25,
                "series": {
                    "win": 2,
                    "loss": 0
                },
                "linescore": [
                    "32",
                    "30",
                    "18",
                    "28"
                ],
                "points": 108
            }
        },
        "officials": [
            "Scott Wall",
            "Tom Washington",
            "Josh Tiven"
        ],
        "timesTied": 5,
        "leadChanges": 9,
        "nugget": null
    },
    {
        "id": 10421,
        "league": "standard",
        "season": 2021,
        "date": {
            "start": "2022-02-13T19:00:00.000Z",
            "end": "2022-02-13T21:27:00.000Z",
            "duration": "2:15"
        },
        "stage": 2,
        "status": {
            "clock": null,
            "halftime": false,
            "short": 3,
            "long": "Finished"
        },
        "periods": {
            "current": 4,
            "total": 4,
            "endOfPeriod": false
        },
        "arena": {
            "name": "TD Garden",
            "city": "Boston",
            "state": "MA",
            "country": "USA"
        },
        "teams": {
            "visitors": {
                "id": 1,
                "name": "Atlanta Hawks",
                "nickname": "Hawks",
                "code": "ATL",
                "logo": "https://upload.wikimedia.org/wikipedia/fr/e/ee/Hawks_2016.png"
            },
            "home": {
                "id": 2,
                "name": "Boston Celtics",
                "nickname": "Celtics",
                "code": "BOS",
                "logo": "https://upload.wikimedia.org/wikipedia/fr/thumb/6/65/Celtics_de_Boston_logo.svg/1024px-Celtics_de_Boston_logo.svg.png"
            }
        },
        "scores": {
            "visitors": {
                "win": 26,
                "loss": 30,
                "series": {
                    "win": 2,
                    "loss": 1
                },
                "linescore": [
                    "28",
                    "27",
                    "23",
                    "17"
                ],
                "points": 95
            },
            "home": {
                "win": 33,
                "loss": 25,
                "series": {
                    "win": 1,
                    "loss": 2
                },
                "linescore": [
                    "17",
                    "28",
                    "42",
                    "18"
                ],
                "points": 105
            }
        },
        "officials": [
            "Kane Fitzgerald",
            "Karl Lane",
            "Brandon Adair"
        ],
        "timesTied": 2,
        "leadChanges": 1,
        "nugget": null
    },
    {
        "id": 10492,
        "league": "standard",
        "season": 2021,
        "date": {
            "start": "2022-03-02T00:30:00.000Z",
            "end": "2022-03-02T03:09:00.000Z",
            "duration": "2:20"
        },
        "stage": 2,
        "status": {
            "clock": null,
            "halftime": false,
            "short": 3,
            "long": "Finished"
        },
        "periods": {
            "current": 4,
            "total": 4,
            "endOfPeriod": false
        },
        "arena": {
            "name": "TD Garden",
            "city": "Boston",
            "state": "MA",
            "country": "USA"
        },
        "teams": {
            "visitors": {
                "id": 1,
                "name": "Atlanta Hawks",
                "nickname": "Hawks",
                "code": "ATL",
                "logo": "https://upload.wikimedia.org/wikipedia/fr/e/ee/Hawks_2016.png"
            },
            "home": {
                "id": 2,
                "name": "Boston Celtics",
                "nickname": "Celtics",
                "code": "BOS",
                "logo": "https://upload.wikimedia.org/wikipedia/fr/thumb/6/65/Celtics_de_Boston_logo.svg/1024px-Celtics_de_Boston_logo.svg.png"
            }
        },
        "scores": {
            "visitors": {
                "win": 29,
                "loss": 32,
                "series": {
                    "win": 2,
                    "loss": 2
                },
                "linescore": [
                    "28",
                    "37",
                    "13",
                    "20"
                ],
                "points": 98
            },
            "home": {
                "win": 37,
                "loss": 27,
                "series": {
                    "win": 2,
                    "loss": 2
                },
                "linescore": [
                    "19",
                    "32",
                    "31",
                    "25"
                ],
                "points": 107
            }
        },
        "officials": [
            "Eric Lewis",
            "Curtis Blair",
            "Scott Twardoski"
        ],
        "timesTied": 7,
        "leadChanges": 6,
        "nugget": null
    }
]

}

CodePudding user response:

for your classes you can try this, but you will not have a root level data in this cases

List<NBAModel> nbaModels=JObject.Parse(json)["response"].ToObject<List<NBAModel>>();
public partial class TeamsHome
    {
        [JsonProperty("id")]
        public long Id { get; set; }

        [JsonProperty("name")]
        public Name Name { get; set; }

        [JsonProperty("nickname")]
        public Nickname Nickname { get; set; }

        [JsonProperty("code")]
        public Code Code { get; set; }

        [JsonProperty("logo")]
        public Uri Logo { get; set; }
    }

    public enum Code { Atl, Bos };

    public enum Name { 
     [EnumMember(Value = "Atlanta Hawks")]
    AtlantaHawks,  
    [EnumMember(Value = "Boston Celtics")]
    BostonCeltics };

    public enum Nickname { Celtics, Hawks };

if you want the whole data you need to add this classes

 Data data = JsonConvert.DeserializeObject<Data>(json);

public partial class Data
    {
        [JsonProperty("get")]
        public string Get { get; set; }

        [JsonProperty("parameters")]
        public Parameters Parameters { get; set; }

        [JsonProperty("errors")]
        public List<object> Errors { get; set; }

        [JsonProperty("results")]
        public long Results { get; set; }

        [JsonProperty("response")]
        public List<NBAModel> Response { get; set; }
    }

    public partial class Parameters
    {
        [JsonProperty("season")]
        
        public long Season { get; set; }

        [JsonProperty("h2h")]
        public string H2H { get; set; }
    }
  • Related