Deserialize placeOrders json response?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • doctormike
    Junior Member
    • Nov 2012
    • 55

    #1

    Deserialize placeOrders json response?

    I'm struggling to create classes enabling me to deserialize the json response to placeOrders. I would use the development teams classes from their example app (which work) but these are related to each other in intuitively convoluted ways at my skill level, and are therefore of limited value for ongoing development. A typical json response is:

    [{"jsonrpc":"2.0","result":{"status":"SUCCESS","mar ketId":"1.115796712","instructionReports":[{"status":"SUCCESS","instruction":{"selectionId":8 443714,"handicap":0.0,"limitOrder":{"size":2.0,"pr ice":1.01,"persistenceType":"LAPSE"},"orderType":" LIMIT","side":"LAY"},"betId":"42088480817","placed Date":"2014-10-07T07:17:48.000Z","averagePriceMatched":0.0,"sizeM atched":0.0}]},"id":1}]

    I've created classes which I thought were consistent with this response but am making mistake(s) somewhere in specifying name/value pairs, as they fail to deserialize. Has anyone created appropriate classes they would be prepared to share (ideally in Visual Basic but C# would be OK).
    With regards
    Mike
  • betdynamics
    Junior Member
    • Sep 2010
    • 534

    #2
    Does this help?

    http://json2csharp.com/

    Comment

    • snooki
      Junior Member
      • Feb 2013
      • 3

      #3
      interesting link there, ty for posting
      found two similar ones which output VB.net as well

      http://www.httputility.net/json-to-c...ipt-class.aspx

      http://jsonutils.com/

      Comment

      • doctormike
        Junior Member
        • Nov 2012
        • 55

        #4
        Json generators

        Many thanks both. These are excellent tools & have enabled me to create the appropriate classes to match the response. However, deserialization of the json object still fails. I'm reproducing request, response, classes, and my attempt to deserialize below. Thank you in advance for your patience and any help you can offer!

        ' REQUEST (offer is successfully lodged so this is OK)
        strRequest = "[{""jsonrpc"":""2.0"",""method"":""SportsAPING/v1.0/placeOrders"",""params"":{""marketId"":" & MyMarket & ",""instructions"":[{""selectionId"":" & MySelection & ",""handicap"": ""0"",""side"": ""LAY"",""orderType"": ""LIMIT"",""limitOrder"": {""size"":" & BetSize & ",""price"":" & BetPrice & ",""persistenceType"": ""LAPSE""}}]},""id"": 1}]"

        ' RESPONSE
        [{"jsonrpc":"2.0","result":{"status":"SUCCESS","mar ketId":"1.115796712","instructionReports":[{"status":"SUCCESS","instruction":{"selectionId":8 443714,"handicap":0.0,"limitOrder":{"size":2.0,"pr ice":1.01,"persistenceType":"LAPSE"},"orderType":" LIMIT","side":"LAY"},"betId":"42088480817","placed Date":"2014-10-07T07:17:48.000Z","averagePriceMatched":0.0,"sizeM atched":0.0}]},"id":1}]

        ' CLASSES
        Imports Newtonsoft.Json
        Imports Newtonsoft.Json.JsonTextReader
        Imports Newtonsoft.Json.JsonTextWriter
        Imports Newtonsoft.Json.Converters
        Public Class RootObject
        <JsonProperty(PropertyName:="jsonrpc")> _
        Public Property jsonrpc() As String
        <JsonProperty(PropertyName:="result")> _
        Public Property result() As Result
        <JsonProperty(PropertyName:="id")> _
        Public Property id() As Integer
        End Class

        Public Class Result
        <JsonProperty(PropertyName:="status")> _
        Public Property status() As String
        <JsonProperty(PropertyName:="marketId")> _
        Public Property marketId() As String
        <JsonProperty(PropertyName:="instructionReports" )> _
        Public Property instructionReports() As List(Of InstructionReport)
        End Class

        Public Class InstructionReport
        <JsonProperty(PropertyName:="status")> _
        Public Property status() As String
        <JsonProperty(PropertyName:="instruction")> _
        Public Property instruction() As Instruction
        <JsonProperty(PropertyName:="betId")> _
        Public Property betId() As String
        <JsonProperty(PropertyName:="placedDate")> _
        Public Property placedDate() As String
        <JsonProperty(PropertyName:="averagePriceMatched") > _
        Public Property averagePriceMatched() As Double
        <JsonProperty(PropertyName:="sizeMatched")> _
        Public Property sizeMatched() As Double
        End Class

        Public Class Instruction
        <JsonProperty(PropertyName:="selectionId")> _
        Public Property selectionId() As Integer
        <JsonProperty(PropertyName:="handicap")> _
        Public Property handicap() As Double
        <JsonProperty(PropertyName:="limitOrder")> _
        Public Property limitOrder() As LimitOrder
        <JsonProperty(PropertyName:="orderType")> _
        Public Property orderType() As String
        <JsonProperty(PropertyName:="side")> _
        Public Property side() As String
        End Class

        Public Class LimitOrder
        <JsonProperty(PropertyName:="size")> _
        Public Property size() As Double
        <JsonProperty(PropertyName:="price")> _
        Public Property price() As Double
        <JsonProperty(PropertyName:="persistenceType")> _
        Public Property persistenceType() As String
        End Class

        ' DESERIALIZATION CODE
        placebetResponse = CreateRequest(strKey, strSessTok, strRequest)
        Dim makebet As String
        makebet = placebetResponse.ToString
        Dim objJson = Newtonsoft.Json.JsonConvert.DeserializeObject(Of RootObject)(makebet)

        Any advice on this would be greatly appreciated!
        With regards
        Mike

        Comment

        • doctormike
          Junior Member
          • Nov 2012
          • 55

          #5
          Found answer

          I've managed to sort this out. Because the json response string is wrapped in square brackets it's interpreted as an array/list instead of a single object. All I had to do was change the deserialization code line to:

          Dim objJson = Newtonsoft.Json.JsonConvert.DeserializeObject(Of List(Of RootObject))(makebet)

          Many thanks for your help all the same. The json decoders are really useful.

          With regards
          Mike

          Comment

          • betdynamics
            Junior Member
            • Sep 2010
            • 534

            #6
            Glad you got it sorted.

            Interestingly, I never get a response that is wrapped in square brackets, so I wonder where they are coming from?

            Comment

            • doctormike
              Junior Member
              • Nov 2012
              • 55

              #7
              Square brackets

              Thanks, betdynamics. Square brackets in the response may be a consequence of something I'm doing in my code - if I find out why it occurs I'll be sure to let you know!
              With regards
              Mike

              Comment

              • doctormike
                Junior Member
                • Nov 2012
                • 55

                #8
                Square brackets wrapper again

                Hi betdynamics, I've just looked at the sample json response for placeOrders in API-NG Reference guide - 28th July.pdf & it also includes square brackets. Their sample request is also wrapped in square brackets, and this is what governed my json request (see earlier post on this thread for my original request spec). I’ve now tried the request without square brackets & the response returns without them, deserializing with my initial code. In the interests of parsimony I’ll now forget about square brackets altogether (at least in this context). Thanks for the prompt!
                With regards
                Mike

                Comment

                Working...
                X