简体   繁体   English

在 web 页面上显示消耗的 API

[英]Display Consumed API on web Page

So ive connected an external API and I want to display it on the page, but im not getting any luck.所以我连接了一个外部 API 我想在页面上显示它,但我没有运气。

I'm using RestSharp so I cant just collect the infromation as such:我正在使用 RestSharp,所以我不能像这样收集信息:

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

Also because i'm using RestSharp, collecting the returned api information with:也因为我使用的是 RestSharp,收集返回的 api 信息:

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.从我看到的情况来看,它需要反序列化,但我不确定在 RestSharp 中是如何完成的。

EDIT 1: This is my model class based on the API results编辑 1:这是我的 model class 基于 API 结果

using Newtonsoft.Json;使用 Newtonsoft.Json;

namespace NBA_API_TEST.Models { public class NBAModel { [JsonProperty("id")] public long Id { get;命名空间 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.从 API 调用应返回 5 个结果。 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 (eg {"name":"value"}) into type 'System.Collections.Generic.List`1[NBA_API_TEST.Models.NBAModel]' because the type requires a JSON array (eg [1,2,3]) to deserialize correctly. JsonSerializationException: Cannot deserialize the current JSON object (eg {"name":"value"}) into type 'System.Collections.Generic.List`1[NBA_API_TEST.Models.NBAModel]' because the type requires a JSON array (eg [ 1,2,3]) 正确反序列化。 To fix this error either change the JSON to a JSON array (eg [1,2,3]) or change the deserialized type so that it is a normal .NET type (eg not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. To fix this error either change the JSON to a JSON array (eg [1,2,3]) or change the deserialized type so that it is a normal .NET type (eg not a primitive type like integer, not a collection type like an数组或列表),可以从 JSON object 反序列化。 JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. JsonObjectAttribute 也可以添加到类型中以强制它从 JSON object 反序列化。

EDIT 2 Update with my JSON用我的 JSON 编辑 2 更新

{
"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
    }
]

} }

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; }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM