我是WP的新手开发人员。我在解析json数据时遇到了一些问题。我从一个网站上获取它,并使用JSON进行解析。网
JSON数据示例:
{"openfooty":{"version":"1.0","response":{"status":"ok"},"teams":{"team":[{"id":"660","cdata":"Arsenal"},{"id:"661","cddata":"Chelsea"}哈姆"},{"id":"670","cdata":"南安普顿"},{"id:"674","cdata:"埃弗顿"},{"id:"675","cdata":"托特纳姆热刺"}"曼城"},{"id":"677","cdata":"诺维奇城"}":"斯托克城"},{"id":"702","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>();}}
但是我没有得到解析的数据。需要帮助!谢谢:)
在声明了所有类的情况下,为什么要像var x= o["openfooty"].First.First["team"];
那样动态。
这适用于
RootObject root = JsonConvert.DeserializeObject<RootObject>(e.Result);
var id = root.openfooty.teams.team[0].id;