Home > Software design >  How to iterate through all keys of json node
How to iterate through all keys of json node

Time:02-11

I'm trying to scrap the key values from this website API and it seems the json format it's not an array. I'm working with console .Net core 6.0 using System.Text.Json.Nodes

The code I'm using is :

    Dim streamData As Stream = Nothing
    Using http As HttpClient = New HttpClient
        Dim url As String = "https://api.hotbit.io/api/v1/market.status24h"

        Dim t As Task(Of Stream) = http.GetStreamAsync(url)
        streamData = t.Result
    End Using

    Dim jsonResponse As JsonNode = JsonNode.Parse(streamData)
    Dim jsonData As JsonNode = jsonResponse("result")

    Dim c As String = String.Empty

    For Each jsonCurrency As JsonNode In jsonData.AsObject
        c  = jsonCurrency("last").ToString   " "
    Next

but I get the error:

Cannot convert type 'KeyValuePair(Of String, JsonNode)' in JsonNode

What Am I doing wrong? Thanks

CodePudding user response:

Create a class to represent your JSON, like this:

Public Class MarketStatus
    Public Property IsChange As Boolean
    Public Property period As Integer
    Public Property open As String
    Public Property last As String
    Public Property high As String
    Public Property low As String
    Public Property volume As String
    Public Property deal As String
    Public Property close As String
    Public Property base_volume As String
    Public Property quote_volume As String
End Class

Public Class Payload
    Public Property _error As Object
    Public Property result As Result
    Public Property id As Integer
End Class

Public Class Result

    <JsonPropertyName("0xBTCBTC")>
    Public Property _0xBTCBTC As MarketStatus

    <JsonPropertyName("0xBTCETH")>
    Public Property _0xBTCETH As MarketStatus

    <JsonPropertyName("0xCASHUSDT")>
    Public Property _0xCASHUSDT As MarketStatus

    <JsonPropertyName("1INCH1D3LUSDT")>
    Public Property _1INCH1D3LUSDT As MarketStatus

    ' etc...
End Class

Now you can deserialize the entire payload by using JsonSerializer.Deserialize or JsonSerializer.DeserializeAsync:

Dim payloadObject = Await JsonSerializer.DeserializeAsync(Of Payload)(streamData)

Update

Per our conversation in the comments of this answer, you want to get the last value of each MarketStatus without having to type each one manually. What you can do is:

  1. Use reflection to get every property of the Result class
  2. Loop over the collection
  3. Use PropertyInfo.GetValue to get the value of the deserialized object

Here is an example using the same variable names as above:

For Each propertyInformation In GetType(Result).GetProperties()
    Dim status = DirectCast(propertyInformation.GetValue(payloadObject.result), MarketStatus)
    Console.WriteLine("{0}.last = {1}", propertyInformation.Name, status.last)
Next

Fiddle: https://dotnetfiddle.net/USaAgc

CodePudding user response:

I solved using

 Dim result As JsonObject = jsonResponse("result").AsObject
    
 For Each kvp In result.AsEnumerable
     c &= kvp.Value("last").ToString & ", "
 Next
  • Related