Results 1 to 4 of 4
  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2013

    Resolved [RESOLVED] Populating treeview from JSON data from Betfair Api? (VB.NET)

    This is my code to fetch all markets from Betfair Api. It gives me hell of a json text and i am finding it difficult to show that data in proper format like treeview or something. Every code i find, is in C# and it is hard for me to convert to VB.NET cauz i am still a noob. How to properly show it to user in treeview so he can expand and view any market prices live?

            Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create("https://api.betfair.com/exchange/betting/rest/v1/en/navigation/menu.json"), HttpWebRequest)
            myHttpWebRequest.Method = "GET"
            myHttpWebRequest.KeepAlive = True
            myHttpWebRequest.Accept = "application/json"
            myHttpWebRequest.Headers("X-Application") = "WcnmFApc9e8M5OA0"
            myHttpWebRequest.Headers("X-Authentication") = "Q3qD4Oj2X/FqiJV2sQSARHjx19fu4MPuptenAEFZquM="
            myHttpWebRequest.Headers("Accept-Encoding") = "gzip,deflate"
            Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse, HttpWebResponse)
            Using responseStream = New GZipStream(myHttpWebResponse.GetResponseStream(), CompressionMode.Decompress)
                Using reader As New StreamReader(responseStream, Encoding.Default)
                    RichTextBox1.Text = reader.ReadToEnd()
                End Using
            End Using
        Catch ex As Exception
        End Try
    Returned JSON text sample is below. It is just a chunk from 3.83 MB plain text data.

    [{"type":"MARKET","name":"AFC Conference Winner","id":"1.140108305","exchangeId":"1","marketType":"TOURNAMENT_WINNER","marketStartTime":"2018-09-07T00:00:00.000Z","numberOfWinners":1},
    {"type":"MARKET","name":"NFC Conference Winner","id":"1.140108307","exchangeId":"1","marketType":"TOURNAMENT_WINNER","marketStartTime":"2018-09-07T00:00:00.000Z","numberOfWinners":1},
    {"type":"MARKET","name":"To Reach The Playoffs","id":"1.140108332","exchangeId":"1","marketType":"SEASON_SPECIALS","marketStartTime":"2018-09-07T00:00:00.000Z","numberOfWinners":12},
    {"type":"MARKET","name":"Super Bowl Winner","id":"1.139778952","exchangeId":"1","marketType":"TOURNAMENT_WINNER","marketStartTime":"2019-02-03T23:30:00.000Z","numberOfWinners":1}],"type":"EVENT","name":"NFL Season 2018/19","id":"28576874","countryCode":""},
    {"children":[{"children":[{"type":"MARKET","name":"Winner 2018-19","id":"1.139846867","exchangeId":"1","marketType":"TOURNAMENT_WINNER","marketStartTime":"2019-01-07T01:00:00.000Z","numberOfWinners":1}],"type":"EVENT","name":"NCAAF FBS Championship 2018-19","id":"28552476","countryCode":"GB"}],"type":"GROUP","name":"NCAAF 2018/19","id":"1575637801"}],"type":"EVENT_TYPE","name":"American Football","id":"6423"},{"children":[{"children":[{"children":[{"type":"MARKET","name":"Outright Winner","id":"1.126411949","exchangeId":"1","marketType":"WINNER","marketStartTime":"2020-08-14T11:00:00.000Z","numberOfWinners":1}],"type":"EVENT","name":"Mens 100m","id":"27901552","countryCode":"GB"}],"type":"GROUP","name":"Tokyo 2020","id":"1576026204"}],"type":"EVENT_TYPE","name":"Athletics","id":"3988"},{"children":[{"children":[{"type":"MARKET","name":"Brownlow/Premiership Double","id":"1.137400431","exchangeId":"1","marketType":"","marketStartTime":"2018-03-22T08:25:00.000Z","numberOfWinners":1},{"type":"MARKET","name":"Coleman Medal","id":"1.139056249","exchangeId":"1","marketType":"","marketStartTime":"2018-03-22T08:25:00.000Z","numberOfWinners":1},{"type":"MARKET","name":"Grand Final Quinella","id":"1.137404350","exchangeId":"1","marketType":"","marketStartTime":"2018-03-22T08:25:00.000Z","numberOfWinners":1},{"type":"MARKET","name":"Least Season Wins","id":"1.137265073","exchangeId":"1","marketType":"UNUSED","marketStartTime":"2018-03-22T08:25:00.000Z","numberOfWinners":1},{"type":"MARKET","name":"Minor Premiers","id":"1.137264827","exchangeId":"1","marketType":"","marketStartTime":"2018-03-22T08:25:00.000Z","numberOfWinners":1},{"type":"MARKET","name":"Premiers 2018","id":"1.134652826","exchangeId":"1","marketType":"","marketStartTime":"2018-03-22T08:25:00.000Z","numberOfWinners":1},{"type":"MARKET","name":"Rising Star","id":"1.138541604","exchangeId":"1","marketType":"","marketStartTime":"2018-03-22T08:25:00.000Z","numberOfWinners":1},{"type":"MARKET","name":"To Reach Grand Final","id":"1.137404337","exchangeId":"1","marketType":"TO_REACH_FINAL","marketStartTime":"2018-03-22T08:25:00.000Z","numberOfWinners":2},{"type":"MARKET","name":"Top 4","id":"1.135039368","exchangeId":"1","marketType":"","marketStartTime":"2018-03-22T08:25:00.000Z","numberOfWinners":4},{"type":"MARKET","name":"Top 8","id":"1.135039350","exchangeId":"1","marketType":"","marketStartTime":"2018-03-22T08:25:00.000Z","numberOfWinners":8},{"type":"MARKET","name":"Top Non Vic Team","id":"1.137490174","exchangeId":"1","marketType":"","marketStartTime":"2018-03-22T08:25:00.000Z","numberOfWinners":1},{"type":"MARKET","name":"Top Victorian Team","id":"1.137490173","exchangeId":"1","marketType":"","marketStartTime":"2018-03-22T08:25:00.000Z","numberOfWinners":1},{"type":"MARKET","name":"Winning Region","id":"1.137404552","exchangeId":"1","marketType":"WINNING_NATIONALITY","marketStartTime":"2018-03-22T08:25:00.000Z","numberOfWinners":1}],"type":"EVENT","name":"AFL 2018 Outrights","id":"28402700","countryCode":"AU"}

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Sep 2013

    Re: Populating treeview from JSON data from Betfair Api? (VB.NET)

    Any help?

  3. #3
    Frenzied Member
    Join Date
    Jul 2011

    Re: Populating treeview from JSON data from Betfair Api? (VB.NET)

    Quote Originally Posted by Terrybogard911 View Post
    .... fetch all markets from Betfair Api. It gives me hell of a json text and i am finding it difficult to show that data in proper format like treeview or something.
    Yeah, it gives a shed load of data. I notice Betfair provide other API endpoints that allow filtering. Maybe they could make your life easier; or maybe you still need all the market data from the Navigation endpoint in order to provide the filter? I don't know, but it might be worth your while to check them out.

    Quote Originally Posted by Terrybogard911 View Post
    Every code i find, is in C# and it is hard for me to convert to VB.NET cauz i am still a noob.
    There are many online and offline converters you can use to help convert from C# to VB. They should at least get you started. It's easier to get help here and elsewhere if you have code in place to ask specific questions about.

    Quote Originally Posted by Terrybogard911 View Post
    How to properly show it to user in treeview so he can expand and view any market prices live?
    I don't know about live, and I don't know how much of the data you need to display, but the following is 'an' approach you could try:

    You'll need Newtonsoft's Json.NET, which you can add to your project via NuGet (from the IDE's Tools Menu, i.e. through Tools/NuGet Package Manager/Manage NuGet Packages for Solution/Browse ), or you could also download and reference it separately if you wished. You'll also need to add a TreeView to your Form, obviously :-).

    Add the following Class to your project:
    VB.NET Code:
    1. Public Class BetfairNgApiObject
    2.     Public Property type As String
    3.     Public Property name As String
    4.     Public Property children As List(Of BetfairNgApiObject)
    5. End Class
    You can add more Properties as needed. They should be named the same as the relevant JSON Property's Key name in the JSON text.

    Some of the objects in the json text may not contain the properties you specify in the BetfairNgApiObject Class definition, but Newtonsoft Json.NET will by default 'ignore' the missing properties and use 'Nothing' to fill in your Class's Property values.

    Given that you say you are new to VB, you should make sure you understand what 'Nothing' means in this context. Start with the documentation here.

    Then to build the TreeView, use code like the following:
    VB.NET Code:
    1. Private Sub BuiildTreeView(menuJson As String)
    2.     Dim ngApiData As BetfairNgApiObject = Newtonsoft.Json.JsonConvert.DeserializeObject(Of BetfairNgApiObject)(menuJson)
    4.     Dim parentNode As TreeNode = TreeView1.Nodes.Add("BetFair")
    5.     populateTreeView(ngApiData.children, parentNode)
    7.     parentNode.Expand()
    8. End Sub
    10. Private Sub populateTreeView(childObjects As List(Of BetfairNgApiObject), parentNode As TreeNode)
    12.     If childObjects IsNot Nothing AndAlso childObjects.Count > 0 Then
    14.         For Each ngObject In childObjects
    15.             Dim nodeText As String = ngObject.name
    17.             Dim childNode As New TreeNode(nodeText)
    18.             parentNode.Nodes.Add(childNode)
    20.             populateTreeView(ngObject.children, childNode)
    21.         Next
    23.     End If
    25. End Sub
    However, I've not worked with a TreeView in over 15 years, so don't ask me where you'd go from there :-).
    Last edited by Inferrd; Feb 27th, 2018 at 08:22 AM. Reason: fixing grandma

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Sep 2013

    Re: Populating treeview from JSON data from Betfair Api? (VB.NET)

    Thanx Man, I really appreciate your detailed response. I am going to do all that one by one. Marking thread as resolved.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts


Click Here to Expand Forum to Full Width

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.