对于模棱两可的标题感到抱歉,但我不知道如何更好地解释。 无论如何,我创建了一个代码来解析c#中的Json,这个结构:
{
"_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"
}
在我的代码中,我可以获得"self"
,"teams"
,"fixtures"
和"leagueTable"
的值。但是如何在Json的结构中看到这个参数有一个关联的链接。我想获取此链接,因为我需要下一次解析数据。
如何获得这个?
实际上,这是我的代码:
private void button_Click(object sender, RoutedEventArgs e)
{
string requestUrl = "http://api.football-data.org/alpha/soccerseasons";
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();
}
List<RootObject> obj = JsonConvert.DeserializeObject<List<RootObject>>(responseText);
LinksClass link = JsonConvert.DeserializeObject<LinksClass>(obj._links);
//89a6ba3f0a6f455b9313107d977f4a43
foreach (var item in obj)
{
Console.WriteLine(
item.Year + " " +
item.League + " " +
item.Caption + " " +
item.NumberOfGames + " " +
item.NumberOfTeams + " " +
item.LastUpdated);
}
}
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 string data { 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; }
}
public class LinksClass
{
[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; }
}
}
你怎么看我创造了这个:
LinksClass link = JsonConvert.DeserializeObject<LinksClass>(obj._links);
和获取此链接的适当类,但我无法得到链接,这种方式我认为是错误的。
答案 0 :(得分:2)
实际上你应该已经拥有第一个反序列化的所有链接值,不需要第二次调用DeserializeObject
:
List<RootObject> obj = JsonConvert.DeserializeObject<List<RootObject>>(responseText);
foreach (var item in obj)
{
//This is how you access the links:
Console.WriteLine(item.Links.Self.href + " ");
}