Parsing Navigation Data

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Lopiner
    Junior Member
    • Feb 2009
    • 117

    #1

    Parsing Navigation Data

    Hi to all.

    Does anyone has the classes for parsing the response from https://api.betfair.com/exchange/bet...tion/menu.json
    I don't understand the structure, i tried the http://json2csharp.com/
    but still cannot understand how the information is organized.

    Thanks
    fooledbyabet.com
  • Lyme58
    Junior Member
    • Nov 2010
    • 5

    #2
    A rather late response, but I'll post my findings in case anyone else will find them helpful.

    Json2CSharp in poor at dealing with this type of nested data, where the items on each level are similar, but not identical - it will create a different class for each level of items.

    Instead you just need one item type with enough fields to cover every element in the data - some will be null depending on the item type for each item.

    Code:
        public class NavigationRoot
        {
            [JsonProperty("children")]
            public List<NavigationItem> Children { get; set; }
    
            [JsonProperty("type")]
            public string Type { get; set; }
    
            [JsonProperty("name")]
            public string Name { get; set; }
    
            [JsonProperty("id")]
            public int Id { get; set; }
        }
    
        public class NavigationItem
        {
            [JsonProperty("children")]
            public List<NavigationItem> Children { get; set; }
    
            [JsonProperty("type")]
            public string Type { get; set; }
    
            [JsonProperty("name")]
            public string Name { get; set; }
    
            [JsonProperty("id")]
            public string Id { get; set; }
    
            [JsonProperty("countryCode")]
            public string CountryCode { get; set; }
    
            [JsonProperty("venue")]
            public string Venue { get; set; }
    
            [JsonProperty("startTime")]
            public string StartTime { get; set; }
    
            [JsonProperty("exchangeId")]
            public string ExchangeId { get; set; }
    
            [JsonProperty("marketType")]
            public string MarketType { get; set; }
    
            [JsonProperty("marketStartTime")]
            public string MarketStartTime { get; set; }
    
            [JsonProperty("numberOfWinners")]
            public int? NumberOfWinners { get; set; }
    
            public override string ToString()
            {
                return string.Format("{0} [{1}]", Name, Type);
            }
        }
    And then having fetched the json text from Betfair, using the Json.net library, you can parse the json text into a NavigationRoot item plus it's children.

    Code:
        var navigationRoot = JsonConvert.DeserializeObject<NavigationRoot>(jsonText);

    Comment

    Working...
    X