JSON Deserialize help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • evan66
    Junior Member
    • Dec 2018
    • 17

    #1

    JSON Deserialize help

    Module bets

    Public Class ABet

    Public jsonrpc As String
    Public result As New currentOrders

    Public id As Long

    End Class

    Public Class currentOrders

    Public betId As String
    Public marketId As String
    Public selectionId As Long
    Public handicap As Double
    Public priceSize As New PriceSize
    .
    .
    .
    .



    responseFromServer = "{""jsonrpc"":""2.0"",""result"":{""currentOrders" ":{""betId"":""158634874286"",""marketId"":""1.156 703637"",""selectionId"":14025157,""handicap"":0.0 ,""priceSize"":{""price"":1.13,""size"":2.0},""bsp Liability"":0.0,""side"":""BACK"",""status"":""EXE CUTION_COMPLETE"",""persistenceType"":""LAPSE"","" orderType"":""LIMIT"",""placedDate"":""2019-03-26T11:12:39.000Z"",""matchedDate"":""2019-03-26T11:12:39.000Z"",""averagePriceMatched"":1.193," "sizeMatched"":2.0,""sizeRemaining"":0.0,""sizeLap sed"":0.0,""sizeCancelled"":0.0,""sizeVoided"":0.0 ,""regulatorCode"":""GIBRALTAR REGULATOR""},""moreAvailable"":false},""id"":1}"

    Dim ThisToken As ABet = Newtonsoft.Json.JsonConvert.DeserializeObject(Of ABet)(responseFromServer)


    Form1.TextBox55.Text = ThisToken.jsonrpc 'CORRECT RESULT OF 2.0


    Form1.TextBox56.Text = ThisToken.result.betId 'EMPTY RESULT



    Why do I get an empty response for betID?
    Tried lots of variations.

    responseFromServer hardcoded in for testing

    Thanks,
    Evan.
  • geoffw123
    Senior Member
    • Mar 2014
    • 250

    #2
    Hi Evan

    I tried to validate your sample json, but the extra spaces and the double speech marks are screwing it up and its a pain to correct to valid json.

    Anyways I think i can see the problem, your data structure definition doesnt look correct to me. Look at your class and variable names for a clue, your class is currentOrders (plural) but in it you have a single betId (singular), this can only represent one bet returned not a bunch of bets.

    listCurrentOrders() returns CurrentOrderSummaryReport

    CurrentOrderSummaryReport is defined as

    public class CurrentOrderSummaryReport
    {
    public List<CurrentOrderSummary> CurrentOrders { get; set; }
    public bool MoreAvailable { get; set; }
    }

    CurrentOrderSummary is defined as

    public string BetId { get; set; } //The bet ID of the original place order.
    public string MarketId { get; set; } // The Market id the order is for.
    public long SelectionId { get; set; } // The selection id the order is for.

    etc etc chopped for brevity

    In other words CurrentOrderSummary is a single bet response, You need need a list of these to give you CurrentOrders (there could be 0 to n bets in the list)

    Your Class/Data definition is missing this list of CurrentOrderSummary bets I think.

    Does that make sense ?

    Geoff


    Last edited by geoffw123; 26-03-2019, 11:22 PM.

    Comment

    • jabe
      Senior Member
      • Dec 2014
      • 705

      #3
      Might it be ThisToken.result.currentOrders.betId ?

      Geoff has a good point too. Since I also used VB.Net, I'll show you my classes:

      Code:
      Class ClassCurrentOrderResult
          Public jsonrpc As String
          Public result As ClassCurrentOrderSummaryReport
      End Class
      
      Public Class ClassCurrentOrderSummaryReport
          ' A container representing search results.
          Public currentOrders() As ClassCurrentOrderSummary ' The list of current orders returned by your query. This will be a valid list (i.e. empty or non-empty but never 'null').
          Public moreAvailable As Boolean ' Indicates whether there are further result items beyond this page. Note that underlying data is highly time-dependent and the subsequent search orders query might return an empty result.
      End Class

      Not sure what's happened with the next one - looks like it's somehow got sorted by data item name, so best ignored. I haven't used it yet.

      Code:
      Public Class ClassCurrentOrderSummary
          ' Summary of a current order. These seem to be in the wrong order, as if someone's sorted them!
          Public averagePriceMatched As Double '     The average price matched at. Voided match fragments are removed from this average calculation.
          Public betId As String ' The bet ID of the original place order.
          Public bspLiability As Double ' Not to be confused with size. This is the liability of a given BSP bet.
          Public handicap As Double ' The handicap of the bet.
          Public marketId As String ' The market id the order is for.
          Public matchedDate As DateTime    ' The date, to the second, of the last matched bet fragment (where applicable)
          Public orderType As Form1.OrderType ' BSP Order type.
          Public persistenceType As Form1.PersistenceType ' What to do with the order at turn-in-play.
          Public placedDate As Date ' The date, to the second, the bet was placed.
          Public priceSize As ClassPriceSize ' The price and size of the bet.
          Public regulatorAuthCode As String      ' The regulator authorisation code.
          Public regulatorCode As String  ' The regulator Code.
          Public selectionId As Long ' The selection id the order is for.
          Public side As Form1.Side ' BACK/LAY
          Public sizeCancelled As Double ' The current amount of this bet that was cancelled.
          Public sizeLapsed As Double ' The current amount of this bet that was lapsed.
          Public sizeMatched As Double ' The current amount of this bet that was matched.
          Public sizeRemaining As Double      ' The current amount of this bet that is unmatched.
          Public sizeVoided As Double ' The current amount of this bet that was voided.
          Public status As Form1.OrderStatus ' Either EXECUTABLE (an unmatched amount remains) or EXECUTION_COMPLETE (no unmatched amount remains).
      End Class



      Last edited by jabe; 26-03-2019, 11:39 PM.

      Comment

      • WTPooh
        Member
        • May 2012
        • 88

        #4
        Hi Evan

        Your responseFromServer is not a valid response.
        Here is valid response:
        "{""jsonrpc"":""2.0"",""result"":{""currentOrd ers" ":[{""betId"":""158634874286"",""marketId"":""1.15 670 3637"",""selectionId"":14025157,""handicap"":0.0 ,""priceSize"":{""price"":1.13,""size"":2.0},"" bsp Liability"":0.0,""side"":""BACK"",""status"":""EXE CUTION_COMPLETE"",""persistenceType"":""LAPSE"","" orderType"":""LIMIT"",""placedDate"":""2019-03-26T11:12:39.000Z"",""matchedDate"":""2019-03-26T11:12:39.000Z"",""averagePriceMatched"":1.193," "sizeMatched"":2.0,""sizeRemaining"":0.0,""siz eLap sed"":0.0,""sizeCancelled"":0.0,""sizeVoided"":0. 0 ,""regulatorCode"":""GIBRALTAR REGULATOR""}],""moreAvailable"":false},""id"":1}"

        Your class definitions are incorrect.
        Here is working code:

        Module Module1

        Sub Main()
        Dim responseFromServer = "{""jsonrpc"":""2.0"",""result"":{""currentOrd ers" ":[{""betId"":""158634874286"",""marketId"":""1.15 670 3637"",""selectionId"":14025157,""handicap"":0.0 ,""priceSize"":{""price"":1.13,""size"":2.0},"" bsp Liability"":0.0,""side"":""BACK"",""status"":""EXE CUTION_COMPLETE"",""persistenceType"":""LAPSE"","" orderType"":""LIMIT"",""placedDate"":""2019-03-26T11:12:39.000Z"",""matchedDate"":""2019-03-26T11:12:39.000Z"",""averagePriceMatched"":1.193," "sizeMatched"":2.0,""sizeRemaining"":0.0,""siz eLap sed"":0.0,""sizeCancelled"":0.0,""sizeVoided"":0. 0 ,""regulatorCode"":""GIBRALTAR REGULATOR""}],""moreAvailable"":false},""id"":1}"
        Dim ThisToken As ABet = Newtonsoft.Json.JsonConvert.DeserializeObject(Of ABet)(responseFromServer)
        End Sub

        Public Class ABet
        Public jsonrpc As String
        Public result As CurrentOrderSummaryReport
        Public id As Long
        End Class

        Public Class CurrentOrderSummaryReport
        Public currentOrders As List(Of CurrentOrderSummary)
        Public moreAvailable As Boolean
        End Class

        Public Class CurrentOrderSummary
        Public betId As String
        Public marketId As String
        Public selectionId As Long
        Public handicap As Double
        'etc
        End Class

        End Module
        Last edited by WTPooh; 26-03-2019, 11:47 PM.

        Comment

        • evan66
          Junior Member
          • Dec 2018
          • 17

          #5
          Thank you all for your help (very quick replies).

          i can see where I’ve gone wrong now.

          I’ll redo the class and your other recommendations in the morning.

          I’ll let you know how it goes.

          thanks again,. I would not have worked it out on my own.
          Evan
          Last edited by evan66; 27-03-2019, 12:14 AM.

          Comment

          • evan66
            Junior Member
            • Dec 2018
            • 17

            #6
            Thank you Geoff, jabe and WTPooh.

            All working now.

            Comment

            • Guest

              #7
              Hmmm first time I have read this thread but I trying to work our why evan you need to "redo the class". All the classes are provided to you as part of the sample code. For example as a Dot.NET C# or VB programmer its here

              https://github.com/betfair/API-NG-sa...sample-code/TO

              You can look up CurrentOrderSummary, but remember the Fuctcion listCurrentOrders returns CurrentOrderSummaryReport which is a list of CurrentOrders and some Moredata.



              Function listCurrentOrders(Optional betIds As ISet(Of [String]) = Nothing, Optional marketIds As ISet(Of [String]) = Nothing, Optional orderProjection As System.Nullable(Of OrderProjection) = Nothing, Optional placedDateRange As TimeRange = Nothing, Optional orderBy As System.Nullable(Of OrderBy) = Nothing, Optional sortDir As System.Nullable(Of SortDir) = Nothing, _
              Optional fromRecord As System.Nullable(Of Integer) = Nothing, Optional recordCount As System.Nullable(Of Integer) = Nothing) As CurrentOrderSummaryReport

              Comment

              • jabe
                Senior Member
                • Dec 2014
                • 705

                #8
                I must admit I found it very informative to create my own classes because having coded everything I knew where all the bits and pieces were. (Though having said that, I had about 20 years working in IT, so I know about how data hangs together).

                Comment

                Working...
                X