0

I made a code that uses the APIs provided by football-data.org, I managed to download the json containing all parameters that I expect to receive. Unless the content in responseText, now I would like to divide the content of responseText in some variables.

string requestUrl = "http://api.football-data.org/alpha/soccerseasons/?season=2014";
HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
request.Method = "GET";
request.ContentType = "application/json";

string responseText;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
using (var responseStream = new StreamReader(response.GetResponseStream()))
{
    responseText = responseStream.ReadToEnd();
} 

Console.WriteLine(responseText);

As you can see the structure of the json also shown in the documentation it is as follows:

Example response:

{
"_links": {
   "self": { "href": "http://api.football-data.org/alpha/soccerseasons/354" },
   "teams": { "href": "http://api.football-data.org/alpha/soccerseasons/teams" },
   "fixtures": { "href": "http://api.football-data.org/alpha/soccerseasons/fixtures" },
   "leagueTable": { "href": "http://api.football-data.org/alpha/soccerseasons/leagueTable" }
},
 "caption": "Premier League 2014/15",
 "league": "PL",
 "year": "2014",
 "numberOfTeams": 20,
 "numberOfGames": 380,
 "lastUpdated": "2014-12-21T10:47:43Z"
}

I would then create variables that once obtained content in responseText, split of all as:

String caption = the caption of json so (Premier League 2014/15)
String league = PL

I do not know if I made clear the idea and if the code that I made is good. I relied on my experience with vb.net strong and now I'm trying to migrate to c# for purposes of study.

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
Harold Finch
  • 576
  • 11
  • 32

1 Answers1

1

You can easily parse your JSON into an object. The structure would look like this (generated using json2csharp, I would edit this a bit as there is some repetitive code):

public class Self
{
    public string href { get; set; }
}

public class Teams
{
    public string href { get; set; }
}

public class Fixtures
{
    public string href { get; set; }
}

public class LeagueTable
{
    public string href { get; set; }
}

public class Links
{
    [JsonProperty("self")]
    public Self Self { get; set; }
    [JsonProperty("teams")]
    public Teams Teams { get; set; }
    [JsonProperty("fixtures")]
    public Fixtures Fixtures { get; set; }
    [JsonProperty("leagueTable")]
    public LeagueTable LeagueTable { get; set; }
}

public class RootObject
{
    [JsonProperty("_links")]
    public Links Links { get; set; }
    [JsonProperty("caption")]
    public string Caption { get; set; }
    [JsonProperty("League")]
    public string League { get; set; }
    [JsonProperty("Year")]
    public string Year { get; set; }
    [JsonProperty("numberOfTeams")]
    public int NumberOfTeams { get; set; }
    [JsonProperty("NumberOfGames")]
    public int NumberOfGames { get; set; }
    [JsonProperty("LastUpdated")]
    public string LastUpdated { get; set; }
}

And then after you read the content as a string, using a serialization framework such as Json.NET, you parse it into an object:

RootObject obj = JsonConvert.DeserializeObject<RootObject>(responseText);
Console.WriteLine(obj.Caption);
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
  • Okay thanks for the answer. I've installed the Json.Net framework but I get an exception in this line of your code: RootObject obj = JsonConvert.DeserializeObject(responseText); in particular the compiler tell me: The type requires a JSON object to deserialize correctly. – Harold Finch Jun 14 '15 at 11:11
  • What is the exact error message? – Yuval Itzchakov Jun 14 '15 at 11:12
  • unhandled exception of type "Newton.Json.JsonSeriealizationException" in Newtonsoft.Json.dll Additional information: Can not deserialize the current JSON array (eg [1,2,3]) into type 'Test.MainWindow RootObject +' because the type Requires a JSON object (eg {"name": "value"}) to deserialize Correctly . – Harold Finch Jun 14 '15 at 11:15
  • Are you trying to deserialize an array of `RootObject` perhaps? Because I see no list in your example JSON. Perhaps try `JsonConvert.DeserializeObject>(responseText);` – Yuval Itzchakov Jun 14 '15 at 11:16