我是WP的新手开发者。我在解析json数据时遇到了一些问题。我从一个网站上获取它并用JSON.Net解析。
JSON数据示例:
{ “openfooty”:{ “版本”: “1.0”, “响应”:{ “状态”: “OK”}, “队”:{ “团队”:[{ “ID”: “660”, “cdata”:“Arsenal”},{“id”:“661”,“cdata”:“Chelsea”},{“id”:“662”,“cdata”:“Manchester United”},{“id” :“663”,“cdata”:“Liverpool”},{“id”:“664”,“cdata”:“纽卡斯尔联队”},{“id”:“665”,“cdata”:“阿斯顿维拉” },{ “ID”: “667”, “CDATA”: “富勒姆”},{ “ID”: “670”, “CDATA”: “南安普敦”},{ “ID”: “674”, “CDATA” :“Everton”},{“id”:“675”,“cdata”:“Tottenham Hotspur”},{“id”:“676”,“cdata”:“Manchester City”},{“id”:“ 677“,”cdata“:”Norwich City“},{”id“:”678“,”cdata“:”West Bromwich Albion“},{”id“:”683“,”cdata“:”桑德兰“} ,{“id”:“684”,“cdata”:“West Ham United”},{“id”:“686”,“cdata”:“Wigan Athletic”},{“id”:“688”,“ cdata“:”Reading“},{”id“:”690“,”cdata“:”Stoke City“},{”id“:”702“,”cdata“:”Queens Park Rangers“},{”id “:”738“,”cdata“:”斯旺西城市“}]}}}
我需要解析团队名称,即“阿森纳”,“切尔西”等,最好用他们的ID并存储在列表中。我所做的课程(使用http://json2csharp.com/#)是:
public class Response
{
public string status { get; set; }
}
public class Team
{
public string id { get; set; }
public string cdata { get; set; }
}
public class Teams
{
public List<Team> team { get; set; }
}
public class Openfooty
{
public string version { get; set; }
public Response response { get; set; }
public Teams teams { get; set; }
}
public class RootObject
{
public Openfooty openfooty { get; set; }
}
我在cs页面中这样做:
public class Loaddata
{
public Loaddata()
{
if (NetworkInterface.GetIsNetworkAvailable())
{
WebClient proxy = new WebClient();
proxy.DownloadStringCompleted += new DownloadStringCompletedEventHandler(proxy_DownloadStringCompleted);
proxy.DownloadStringAsync(new Uri("http://api.openfooty.org/1.0/league.getTeams?api_key=4&league_id=8&format=json"));
}
}
void proxy_DownloadStringCompleted(object sender,DownloadStringCompletedEventArgs e)
{
if (e.Error != null) return;
{
var o = JObject.Parse(e.Result);
var x= o["openfooty"].First.First["team"];
string name = x.Value<string>();}}
但我没有得到解析数据。需要帮助!谢谢:))
答案 0 :(得分:1)
为什么当你声明了所有类时,你会像var x= o["openfooty"].First.First["team"];
一样动态。
这是有效的
RootObject root = JsonConvert.DeserializeObject<RootObject>(e.Result);
var id = root.openfooty.teams.team[0].id;