I'm a novice developer for WP. I have some problems with parsing json data. I take it from a website and parse with JSON.Net.
Example of JSON data:
{"openfooty":{"version":"1.0","response":{"status":"ok"},"teams":{"team":[{"id":"660","cdata":"Arsenal"},{"id":"661","cdata":"Chelsea"},{"id":"662","cdata":"Manchester United"},{"id":"663","cdata":"Liverpool"},{"id":"664","cdata":"Newcastle United"},{"id":"665","cdata":"Aston Villa"},{"id":"667","cdata":"Fulham"},{"id":"670","cdata":"Southampton"},{"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":"Sunderland"},{"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":"Swansea City"}]}}}
I need to parse the team names , ie "arsenal", "chelsea" etc , preferably with their ids and store in a list. The classes i've made (using http://json2csharp.com/# ) are :
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; }
}
I do this in the cs page :
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>();}}
but i dont get the parsed data. help required ! Thanks :)
Why do you go dynamic way like var x= o["openfooty"].First.First["team"];
when you have all the classes declared.
this works
RootObject root = JsonConvert.DeserializeObject<RootObject>(e.Result);
var id = root.openfooty.teams.team[0].id;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.