2015-06-14 54 views
-1

我提出,使用由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#的研究目的。

+0

嘗試搜索「C#解析json」或「C#反序列化json到對象」。 – CodeCaster

回答

1

您可以輕鬆地分析你的JSON成一個對象。該結構是這樣的(使用json2csharp產生的,我會因爲有一些重複的代碼編輯這一點):

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

然後你閱讀的內容作爲一個字符串後,使用一個序列化框架如Json.NET ,你解析成一個對象:

RootObject obj = JsonConvert.DeserializeObject<RootObject>(responseText); 
Console.WriteLine(obj.Caption); 
+0

好的,謝謝你的答案。我已經安裝了Json.Net框架,但我得到了這行代碼中的一個例外:RootObject OBJ = JsonConvert.DeserializeObject (responseText的);特別是編譯器告訴我:該類型需要一個JSON對象才能正確地反序列化。 –

+0

什麼是確切的錯誤信息? –

+0

Newtonsoft.Json.dll中類型爲「Newton.Json.JsonSeriealizationException」的未處理異常 附加信息:無法將當前JSON數組(例如[1,2,3])反序列化爲類型'Test.MainWindow RootObject +',因爲的類型要求一個JSON對象(例如{「名稱」:「值」})來正確反序列化。 –