如何在解析时过滤 JSON 中的一些结果
How to filter some results in JSON while parsing it
我正在使用以下 .net 框架代码从 this page 解析 JSON:
Dim jsonString As String
Using http As HttpClient = New HttpClient
Dim url As String = "https://api-cloud.bitmart.com/account/v1/currencies"
Dim streamData As Stream = Await http.GetStreamAsync(url)
Using sr As StreamReader = New StreamReader(streamData)
jsonString = sr.ReadToEnd
End Using
streamData.Close()
End Using
Dim jsonResponse As JObject = JObject.Parse(jsonString)
Dim jsonData As JObject = CType(jsonResponse("data"), JObject)
Dim jsonCurrencies As JArray = CType(jsonData("currencies"), JArray)
Dim c As String = String.Empty
Dim n As String = String.Empty
For Each jsonCurrency As JObject In jsonCurrencies
c += jsonCurrency("currency").ToString + " - " & jsonCurrency("network").ToString + vbLf
Next
RichTextBox1.Text += c
它工作得很好,但我认为所有的结果都是“真实的,真实的”
但现在我播种也有像
这样的结果
"withdraw_enabled":false,"deposit_enabled":false}
which I really don't need to be parsed.
我感兴趣的结果首先是
"withdraw_enabled":true,"deposit_enabled":true}
在类似下面的情况下,我想解析它们,但我想知道提取和存款之间的哪一个是正确的and/or false
"withdraw_enabled":false,"deposit_enabled":true}
总结:
如果JSON是
[{"currency":"DFC","name":"DefiConnect","network":"BEP20","withdraw_enabled":true,"deposit_enabled":true},
输出是
DFC - BEP20 ( my code is already working for this case)
如果JSON是
{"currency":"HOG","name":"HOG Finance","network":"BEP20(BSC)","withdraw_enabled":true,"deposit_enabled":false},
输出应该是
HOG - BEP20(BSC) [withdraw: Yes - deposit: NO]
如果 JSON 是
{"currency":"SGE","name":"Society of Galactic Exploration","network":"ERC20","withdraw_enabled":false,"deposit_enabled":false}
我不需要解析它。
你觉得有可能吗?
希望我已经清楚了。
谢谢
您可以反序列化为实际的 class。
,而不是使用通用 JObject
Public Class RootObject
Public Property Message As String
Public Property Code As Integer
Public Property Trace As String
Public Property Data As Data
End Class
Public Class Data
Public Property Currencies As Currency()
End Class
Public Class Currency
Public Property Currency As String
Public Property Name As String
Public Property Network As String
Public Property Withdraw_enabled As Boolean
Public Property Deposit_enabled As Boolean
End Class
然后在您的处理过程中,只需检查 属性 是否为真。
Dim root = JsonConvert.DeserializeObject(Of RootObject)(jsonString)
Dim builder As New StringBuilder()
For Each currency As Currency In root.Data.Currencies
'Only consider the currency if at least one enabled is true
If currency.Withdraw_enabled OrElse currency.Deposit_enabled Then
builder.Append(currency.Currency)
builder.Append(" - ")
builder.AppendLine(currency.Network)
End If
Next
RichTextBox1.Append(builder.ToString())
我正在使用以下 .net 框架代码从 this page 解析 JSON:
Dim jsonString As String
Using http As HttpClient = New HttpClient
Dim url As String = "https://api-cloud.bitmart.com/account/v1/currencies"
Dim streamData As Stream = Await http.GetStreamAsync(url)
Using sr As StreamReader = New StreamReader(streamData)
jsonString = sr.ReadToEnd
End Using
streamData.Close()
End Using
Dim jsonResponse As JObject = JObject.Parse(jsonString)
Dim jsonData As JObject = CType(jsonResponse("data"), JObject)
Dim jsonCurrencies As JArray = CType(jsonData("currencies"), JArray)
Dim c As String = String.Empty
Dim n As String = String.Empty
For Each jsonCurrency As JObject In jsonCurrencies
c += jsonCurrency("currency").ToString + " - " & jsonCurrency("network").ToString + vbLf
Next
RichTextBox1.Text += c
它工作得很好,但我认为所有的结果都是“真实的,真实的” 但现在我播种也有像
这样的结果"withdraw_enabled":false,"deposit_enabled":false} which I really don't need to be parsed.
我感兴趣的结果首先是
"withdraw_enabled":true,"deposit_enabled":true}
在类似下面的情况下,我想解析它们,但我想知道提取和存款之间的哪一个是正确的and/or false
"withdraw_enabled":false,"deposit_enabled":true}
总结:
如果JSON是
[{"currency":"DFC","name":"DefiConnect","network":"BEP20","withdraw_enabled":true,"deposit_enabled":true},
输出是
DFC - BEP20 ( my code is already working for this case)
如果JSON是
{"currency":"HOG","name":"HOG Finance","network":"BEP20(BSC)","withdraw_enabled":true,"deposit_enabled":false},
输出应该是
HOG - BEP20(BSC) [withdraw: Yes - deposit: NO]
如果 JSON 是
{"currency":"SGE","name":"Society of Galactic Exploration","network":"ERC20","withdraw_enabled":false,"deposit_enabled":false}
我不需要解析它。
你觉得有可能吗? 希望我已经清楚了。 谢谢
您可以反序列化为实际的 class。
,而不是使用通用 JObject Public Class RootObject
Public Property Message As String
Public Property Code As Integer
Public Property Trace As String
Public Property Data As Data
End Class
Public Class Data
Public Property Currencies As Currency()
End Class
Public Class Currency
Public Property Currency As String
Public Property Name As String
Public Property Network As String
Public Property Withdraw_enabled As Boolean
Public Property Deposit_enabled As Boolean
End Class
然后在您的处理过程中,只需检查 属性 是否为真。
Dim root = JsonConvert.DeserializeObject(Of RootObject)(jsonString)
Dim builder As New StringBuilder()
For Each currency As Currency In root.Data.Currencies
'Only consider the currency if at least one enabled is true
If currency.Withdraw_enabled OrElse currency.Deposit_enabled Then
builder.Append(currency.Currency)
builder.Append(" - ")
builder.AppendLine(currency.Network)
End If
Next
RichTextBox1.Append(builder.ToString())