0

I am receiving a JSON result from this API web page (https://flagrantflop.com/api/endpoint.php?api_key=13b6ca7fa0e3cd29255e044b167b01d7&scope=team_stats&season=2019-2020&season_type=regular&team_name=Atlanta%20Hawks)

Using the RestSharp library, so far I've got this:

var client = new RestClient("https://flagrantflop.com/api/endpoint.php?api_key=13b6ca7fa0e3cd29255e044b167b01d7&scope=team_stats&season=2019-2020&season_type=regular&team_name=");

var request = new RestRequest("Atlanta Hawks", DataFormat.Json);

var response = client.Get(request);

I have tested the URL and the request part that specifies the team and both work.

I know there are a number of methods of deserializing the JSON, however not sure the best way.

Brady Harper
  • 235
  • 1
  • 3
  • 17
  • Use something like http://json2csharp.com to generate a C# class from the JSON. Then use Newtonsoft JSON.NET to deserialize the response into that class – Steve Feb 04 '20 at 12:36
  • @Steve Cheers. I've implemented the class, however when doing `RootObject data = JsonConvert.DeserializeObject(response); ` I get told I can't convert the IRestResponse to a string. And adding .Content onto the response gives me the error "Unexpected character encountered while parsing value". I think there's an error with the JSON coming in maybe? But it looks fine on the web page – Brady Harper Feb 04 '20 at 12:43
  • Something like : `client.Execute(request, (response) => { RootObject data = JsonConvert.DeserializeObject(response.Content); });` Though you might want to declare RootObject outside the call. – Steve Feb 04 '20 at 12:55
  • Or possibly `RootObject data = JsonConvert.DeserializeObject(response.Content);` – Steve Feb 04 '20 at 12:56
  • Or more simply see this answer : https://stackoverflow.com/questions/47429596/deserialize-json-array-into-c-sharp-structure-with-restsharp – Steve Feb 04 '20 at 13:01
  • @Steve They still have the same error. Seems like the JSON coming in isn't recognised as JSON. I added some breakpoints and had a look into the content that is being returned from the GET call, and it's just the HTML of the website, for some reason the program is completely ignoring the API URL – Brady Harper Feb 04 '20 at 13:04
  • If thats the case then it looks like the website isn't picking up that you want JSON back. Most examples I've seen use the `.Execute()` method. The `DataFormat.Json` looks a bit suspect. See this Q&A https://stackoverflow.com/questions/11803485/how-to-parse-json-using-restsharp – Steve Feb 04 '20 at 13:20

1 Answers1

0

The request isn't working because the argument you're supplying in RestRequest is treated as its own page stemming off the base URI.

You can verify that by calling client.BuildUri(request) with your current setup―you'll see that the resolved URL is https://flagrantflop.com/api/Atlanta Hawks, which is why you weren't getting the proper JSON response. I recommend rewriting the request like this, but there are other valid ways:

var client = new RestClient("https://flagrantflop.com/api/")
    .AddDefaultQueryParameter("api_key", "13b6ca7fa0e3cd29255e044b167b01d7")
    .AddDefaultQueryParameter("scope", "team_stats")
    .AddDefaultQueryParameter("season", "2019-2020")
    .AddDefaultQueryParameter("season_type", "regular");

var request = new RestRequest("endpoint.php")
    .AddQueryParameter("team_name", "Atlanta Hawks");

After that, you can have RestSharp automatically deserialize your response:

RootObject response = client.Get<RootObject>(request);

By default, this uses SimpleJson to deserialize your object.

Roth
  • 149
  • 2
  • 7