我是 WP 的新手开发人员。我在解析 json 数据时遇到了一些问题。我从网站上获取它并使用 JSON.Net 进行解析。
JSON数据示例:
{"openfooty":{"version":"1.0","response":{"status":"ok"},"teams":{"team":[{"id":"660","cdata" :"阿森纳"},{"id":"661","cdata":"切尔西"},{"id":"662","cdata":"曼联"},{"id":"663 ","cdata":"利物浦"},{"id":"664","cdata":"纽卡斯尔联队"},{"id":"665","cdata":"阿斯顿维拉"},{ "id":"667","cdata":"富勒姆"},{"id":"670","cdata":"南安普顿"},{"id":"674","cdata":"埃弗顿"},{"id":"675","cdata":"托特纳姆热刺队"},{"id":"676","cdata":"曼城"},{"id":"677","cdata":"诺维奇城"},{"id ":"678","cdata":"西布罗姆维奇"},{"id":"683","cdata":"桑德兰"},{"id":"684","cdata":"西Ham United"},{"id":"686","cdata":"维冈竞技"},{"id":"688","cdata":"Reading"},{"id":"690" ,"cdata":"斯托克城"},{"id":"702","cdata":"皇后公园巡游者队"},{"id":"738","cdata":"斯旺西城"}] }}}},{"id":"676","cdata":"曼彻斯特城"},{"id":"677","cdata":"诺维奇城"},{"id":"678"," cdata":"西布罗姆维奇"},{"id":"683","cdata":"桑德兰"},{"id":"684","cdata":"西汉姆联队"},{" id":"686","cdata":"维冈竞技"},{"id":"688","cdata":"阅读"},{"id":"690","cdata":"斯托克城市"},{"id":"702","cdata":"皇后公园巡游者队"},{"id":"738","cdata":"斯旺西市"}]}}}},{"id":"676","cdata":"曼彻斯特城"},{"id":"677","cdata":"诺维奇城"},{"id":"678"," cdata":"西布罗姆维奇"},{"id":"683","cdata":"桑德兰"},{"id":"684","cdata":"西汉姆联队"},{" id":"686","cdata":"维冈竞技"},{"id":"688","cdata":"阅读"},{"id":"690","cdata":"斯托克城市"},{"id":"702","cdata":"皇后公园巡游者队"},{"id":"738","cdata":"斯旺西市"}]}}}{"id":"677","cdata":"诺维奇城"},{"id":"678","cdata":"西布罗姆维奇"},{"id":"683","cdata ":"桑德兰"},{"id":"684","cdata":"西汉姆联"},{"id":"686","cdata":"维冈竞技"},{"id" :"688","cdata":"阅读"},{"id":"690","cdata":"斯托克城"},{"id":"702","cdata":"女王公园巡游者"},{"id":"738","cdata":"斯旺西市"}]}}}{"id":"677","cdata":"诺维奇城"},{"id":"678","cdata":"西布罗姆维奇"},{"id":"683","cdata ":"桑德兰"},{"id":"684","cdata":"西汉姆联"},{"id":"686","cdata":"维冈竞技"},{"id" :"688","cdata":"阅读"},{"id":"690","cdata":"斯托克城"},{"id":"702","cdata":"女王公园巡游者"},{"id":"738","cdata":"斯旺西市"}]}}}{"id":"683","cdata":"桑德兰"},{"id":"684","cdata":"西汉姆联"},{"id":"686","cdata" :"维冈竞技"},{"id":"688","cdata":"Reading"},{"id":"690","cdata":"斯托克城"},{"id":" 702","cdata":"皇后公园巡游者"},{"id":"738","cdata":"斯旺西城"}]}}}{"id":"683","cdata":"桑德兰"},{"id":"684","cdata":"西汉姆联"},{"id":"686","cdata" :"维冈竞技"},{"id":"688","cdata":"Reading"},{"id":"690","cdata":"斯托克城"},{"id":" 702","cdata":"皇后公园巡游者"},{"id":"738","cdata":"斯旺西城"}]}}}690","cdata":"斯托克城"},{"id":"702","cdata":"皇后公园巡游者队"},{"id":"738","cdata":"斯旺西城" }]}}}690","cdata":"斯托克城"},{"id":"702","cdata":"皇后公园巡游者队"},{"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>();}}
但我没有得到解析的数据。需要帮助!谢谢 :)