0

I'm trying to deserialize JSON from a url using the following code. I'm getting an empty object even though I'm downloading a valid JSON string.

public static void tryjson() {
    string json = new WebClient().DownloadString("http://www.sofascore.com/football/livescore/json");
    SportItem sportItem = JsonConvert.DeserializeObject<SportItem>(json);
}

Classes:

public class Sport {
    public int id { get; set; }
    public string name { get; set; }
    public string slug { get; set; }
}

public class Tournament2 {
    public int id { get; set; }
    public string name { get; set; }
    public string slug { get; set; }
    public int priority { get; set; }
    public int order { get; set; }
    public int uniqueId { get; set; }
    public string uniqueName { get; set; }
}

public class Category {
    public int id { get; set; }
    public string name { get; set; }
    public string slug { get; set; }
    public int priority { get; set; }
    public List<object> mcc { get; set; }
    public string flag { get; set; }
}

public class Season {
    public int id { get; set; }
    public string name { get; set; }
    public string slug { get; set; }
    public string year { get; set; }
}

public class HomeScore {
    public int current { get; set; }
    public int period1 { get; set; }
    public int normaltime { get; set; }
    public int? aggregated { get; set; }
    public int? penalties { get; set; }
    public int? overtime { get; set; }
}

public class AwayScore {
    public int current { get; set; }
    public int period1 { get; set; }
    public int normaltime { get; set; }
    public int? aggregated { get; set; }
    public int? penalties { get; set; }
    public int? overtime { get; set; }
}

public class Status {
    public int code { get; set; }
    public string type { get; set; }
}

public class Changes {
    public string changeDate { get; set; }
    public List<object> changes { get; set; }
    public int changeTimestamp { get; set; }
    public bool hasExpired { get; set; }
    public bool hasHomeChanges { get; set; }
    public bool hasAwayChanges { get; set; }
}

public class RoundInfo {
    public int round { get; set; }
    public string name { get; set; }
}

public class Sport2 {
    public int id { get; set; }
    public string name { get; set; }
    public string slug { get; set; }
}

public class HomeTeam {
    public int id { get; set; }
    public string name { get; set; }
    public string slug { get; set; }
    public string gender { get; set; }
    public List<object> subTeams { get; set; }
}

public class AwayTeam {
    public int id { get; set; }
    public string name { get; set; }
    public string slug { get; set; }
    public string gender { get; set; }
    public List<object> subTeams { get; set; }
}

public class Odds {
    public int id { get; set; }
    public double odds1 { get; set; }
    public double oddsX { get; set; }
    public double odds2 { get; set; }
    public double odds1X { get; set; }
    public double oddsX2 { get; set; }
    public double odds12 { get; set; }
    public double liveOdds1 { get; set; }
    public double liveOddsX { get; set; }
    public double liveOdds2 { get; set; }
    public double liveOdds1X { get; set; }
    public double liveOddsX2 { get; set; }
    public double liveOdds12 { get; set; }
    public string fractionalOdds1 { get; set; }
    public string fractionalOddsX { get; set; }
    public string fractionalOdds2 { get; set; }
    public string fractionalOdds1X { get; set; }
    public string fractionalOddsX2 { get; set; }
    public string fractionalOdds12 { get; set; }
    public string liveFractionalOdds1 { get; set; }
    public string liveFractionalOddsX { get; set; }
    public string liveFractionalOdds2 { get; set; }
    public string liveFractionalOdds1X { get; set; }
    public string liveFractionalOddsX2 { get; set; }
    public string liveFractionalOdds12 { get; set; }
    public string americanOdds1 { get; set; }
    public string americanOddsX { get; set; }
    public string americanOdds2 { get; set; }
    public string americanOdds1X { get; set; }
    public string americanOddsX2 { get; set; }
    public string americanOdds12 { get; set; }
    public string liveAmericanOdds1 { get; set; }
    public string liveAmericanOddsX { get; set; }
    public string liveAmericanOdds2 { get; set; }
    public string liveAmericanOdds1X { get; set; }
    public string liveAmericanOddsX2 { get; set; }
    public string liveAmericanOdds12 { get; set; }
    public int odds1Change { get; set; }
    public int oddsXChange { get; set; }
    public int odds2Change { get; set; }
    public int odds1XChange { get; set; }
    public int oddsX2Change { get; set; }
    public int odds12Change { get; set; }
    public string bet365OddsId1 { get; set; }
    public string bet365OddsIdX { get; set; }
    public string bet365OddsId2 { get; set; }
    public string bet365OddsId1X { get; set; }
    public string bet365OddsIdX2 { get; set; }
    public string bet365OddsId12 { get; set; }
    public string betSlipLink1 { get; set; }
    public string betSlipLinkX { get; set; }
    public string betSlipLink2 { get; set; }
    public string betSlipLink1X { get; set; }
    public string betSlipLinkX2 { get; set; }
    public string betSlipLink12 { get; set; }
    public bool liveOddsEnabled { get; set; }
    public bool liveDoubleChanceOddsEnabled { get; set; }
}

public class Event {
    public int id { get; set; }
    public string customId { get; set; }
    public HomeScore homeScore { get; set; }
    public AwayScore awayScore { get; set; }
    public Status status { get; set; }
    public int winnerCode { get; set; }
    public Changes changes { get; set; }
    public RoundInfo roundInfo { get; set; }
    public Sport2 sport { get; set; }
    public HomeTeam homeTeam { get; set; }
    public AwayTeam awayTeam { get; set; }
    public Odds odds { get; set; }
    public bool hasHighlights { get; set; }
    public bool hasHighlightsStream { get; set; }
    public bool hasEventPlayerStatistics { get; set; }
    public bool hasEventPlayerHeatMap { get; set; }
    public int rowCount { get; set; }
    public int homeRedCards { get; set; }
    public int awayRedCards { get; set; }
    public string statusDescription { get; set; }
    public bool hasLiveForm { get; set; }
    public string name { get; set; }
    public string startTime { get; set; }
    public string formatedStartDate { get; set; }
    public int startTimestamp { get; set; }
    public string slug { get; set; }
    public bool hasLineupsList { get; set; }
    public bool hasOdds { get; set; }
    public bool hasLiveOdds { get; set; }
    public bool hasFirstToServe { get; set; }
    public bool hasDraw { get; set; }
    public bool isSyncable { get; set; }
    public int? aggregatedWinnerCode { get; set; }
}

public class Tournament {
    public Tournament2 tournament { get; set; }
    public Category category { get; set; }
    public Season season { get; set; }
    public bool hasEventPlayerStatistics { get; set; }
    public bool hasEventPlayerHeatMap { get; set; }
    public List<Event> events { get; set; }
}

public class SportItem {
    public Sport sport { get; set; }
    public int rows { get; set; }
    public List<Tournament> tournaments { get; set; }
}

public class Params {
    public string sport { get; set; }
    public object category { get; set; }
    public string date { get; set; }
}

public class RootObject {
    public SportItem sportItem { get; set; }
    public Params @params { get; set; }
    public bool isShortDate { get; set; }
}

What am I doing wrong?

2
  • Seems like you used json2csharp.com . RootObject is the root class. try JsonConvert.DeserializeObject<RootObject>(json); Commented Oct 22, 2015 at 20:50
  • worked ... thank you Commented Oct 22, 2015 at 20:54

2 Answers 2

1

Try this:

System.Net.WebClient wc = new System.Net.WebClient();
wc.DownloadStringCompleted += (s, u) =>
{
    DownloadStringCompleted(s, u);
};
wc.DownloadStringAsync(new Uri("http://www.sofascore.com/football/livescore/json"));


private void DownloadStringCompleted(object s, DownloadStringCompletedEventArgs u)
{
    try
    {
        var Item = JsonConvert.DeserializeObject<RootObject>(u.Result.ToString());
    }
    catch (Exception ex)
    { }
}   
Sign up to request clarification or add additional context in comments.

Comments

1

It looks like you need to parse into the json object first. Example:-

var data = (JObject)JsonConvert.DeserializeObject(resultJson);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.