Hi, I'm parsin a Json API
which is basically an API from one crypto coin of a crypto exchange. Now the crypto coin is under maintenance, so the trading is closed and the Json is changed.
The JSON when the trading coin is open is:
{"error":null,"result":{"KIBAUSDT":{"limit":100,"offset":0,"total":1,"records":[{"user":2996434,"id":82651361103,"market":"KIBAUSDT","freeze":"0.001","fee_stock":"","source":"web","type":1,"side":2,"ctime":1645022087.403207,"mtime":1645022087.403207,"price":"0.00001","amount":"100","taker_fee":"0.0035","maker_fee":"-0.003","left":"100","deal_stock":"0","deal_money":"0","deal_fee":"0","alt_fee":"0","deal_fee_alt":"0","status":0}]}},"id":305636871}
While now that the trading is in maintenance, the Json is:
{"error":{"code":6,"message":"RET_TRADE_NOT_OPENKIBAUSDT"},"result":null,"id":0}
The code I was using in order to parse the JSON when the trading is open is:
Option Strict On
Imports System
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Converters
Imports System.Runtime.CompilerServices
Namespace OrdiniAperti
Partial Public Class Openorders
<JsonProperty("error")>
Public Property [Error] As Object
<JsonProperty("result")>
Public Property Result As Result
<JsonProperty("id")>
Public Property Id As Double
End Class
Partial Public Class Result
<JsonProperty("KIBAUSDT")>
Public Property Kibausdt As Kibausdt
End Class
Partial Public Class Kibausdt
<JsonProperty("limit")>
Public Property Limit As Double
<JsonProperty("offset")>
Public Property Offset As Double
<JsonProperty("total")>
Public Property Total As Double
<JsonProperty("records")>
Public Property Records As Record()
End Class
Partial Public Class Record
<JsonProperty("user")>
Public Property User As Double
<JsonProperty("id")>
Public Property Id As Double
<JsonProperty("market")>
Public Property Market As String
<JsonProperty("freeze")>
Public Property Freeze As String
<JsonProperty("fee_stock")>
Public Property FeeStock As String
<JsonProperty("source")>
Public Property Source As String
<JsonProperty("type")>
Public Property Type As Double
<JsonProperty("side")>
Public Property Side As Double
<JsonProperty("ctime")>
Public Property Ctime As Double
<JsonProperty("mtime")>
Public Property Mtime As Double
<JsonProperty("price")>
Public Property Price As String
<JsonProperty("amount")>
<JsonConverter(GetType(ParseStringConverter))>
Public Property Amount As Double
<JsonProperty("taker_fee")>
Public Property TakerFee As String
<JsonProperty("maker_fee")>
Public Property MakerFee As String
<JsonProperty("left")>
<JsonConverter(GetType(ParseStringConverter))>
Public Property Left As Double
<JsonProperty("deal_stock")>
<JsonConverter(GetType(ParseStringConverter))>
Public Property DealStock As Double
<JsonProperty("deal_money")>
<JsonConverter(GetType(ParseStringConverter))>
Public Property DealMoney As Double
<JsonProperty("deal_fee")>
<JsonConverter(GetType(ParseStringConverter))>
Public Property DealFee As Double
<JsonProperty("alt_fee")>
<JsonConverter(GetType(ParseStringConverter))>
Public Property AltFee As Double
<JsonProperty("deal_fee_alt")>
<JsonConverter(GetType(ParseStringConverter))>
Public Property DealFeeAlt As Double
<JsonProperty("status")>
Public Property Status As Double
End Class
Partial Public Class Openorders
Public Shared Function FromJson(ByVal json As String) As Openorders
Return JsonConvert.DeserializeObject(Of Openorders)(json, Settings)
End Function
End Class
Public Module Serialize
<Extension()>
Public Function ToJson(ByVal self As Openorders) As String
Return JsonConvert.SerializeObject(self, Settings)
End Function
End Module
Friend Module Converter
Public ReadOnly Settings As JsonSerializerSettings = New JsonSerializerSettings With {
.MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
.DateParseHandling = DateParseHandling.None
}
'System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture
End Module
Friend Class ParseStringConverter
Inherits JsonConverter
Public Overrides Function CanConvert(ByVal t As Type) As Boolean
Return t Is GetType(Double) OrElse t Is GetType(Double?)
End Function
Public Overrides Function ReadJson(ByVal reader As JsonReader, ByVal t As Type, ByVal existingValue As Object, ByVal serializer As JsonSerializer) As Object
If reader.TokenType = JsonToken.Null Then Return Nothing
Dim value = serializer.Deserialize(Of String)(reader)
Dim l As Double
If Double.TryParse(value, l) Then
Return l
End If
Throw New Exception("Cannot unmarshal type Double")
End Function
Public Overrides Sub WriteJson(ByVal writer As JsonWriter, ByVal untypedValue As Object, ByVal serializer As JsonSerializer)
If untypedValue Is Nothing Then
serializer.Serialize(writer, Nothing)
Return
End If
Dim value = CLng(untypedValue)
serializer.Serialize(writer, value.ToString())
Return
End Sub
Public Shared ReadOnly Singleton As ParseStringConverter = New ParseStringConverter()
End Class
End Namespace
and in a button:
Dim json = Await wc1.DownloadStringTaskAsync(TextBox1.Text)
'deserialize the json
Dim rootObj = Openorders.FromJson(json)
'navigate to Kibausdt ( when trading is open)
Dim kibausdt = rootObj.Result.Kibausdt
'navigate to Id ( when trade is closed)
Dim opentrading = rootObj.Id
If opentrading = 0 Then
RichTextBox3.Text = "trading not open"
Else
If kibausdt.Total = 0 Then
RichTextBox3.Text = "0 opened orders"
Else
RichTextBox3.Clear()
'loop through records
For Each record In kibausdt.Records
Dim side As String = record.Side.ToString()
ecc
So as you can see from the code in the button, I'm trying to navigate to Id because id become to 0 when the trading is not open. But unfortunately I'm getting an error at debug, the app goes in break mode and it thrown the following error : System.Reflection.TargetInvocationException: 'exception has been thrown by the target of an invocation
How it is possible? I'm navigate correctly to the Id, so I'm excpeting it to work..
Any idea?
Thanks