我提出,使用由football-data.org提供的API代碼,我設法下載包含我期望得到的所有參數的JSON。除非responseText中的內容,現在我想在一些變量中分割responseText的內容。如何保存JSON屬性在一些C#變量
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);
正如你所看到的是如下它在文檔中也顯示了JSON的結構:
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"
}
我會再創造,一旦取得內容的responseText的變量,所有的分裂:
String caption = the caption of json so (Premier League 2014/15)
String league = PL
我不知道如果我明確的想法,如果我做的代碼是好的。我依靠我的經驗與強大的vb.net,現在我正試圖遷移到C#的研究目的。
嘗試搜索「C#解析json」或「C#反序列化json到對象」。 – CodeCaster