How to get all the values from a json array

Mattia Fanti 356 Reputation points
2022-02-04T22:25:46.993+00:00

I'm trying to get all the keys from a json array. The link of the json is: this one

and the code I'm using is:

Imports System.Net.Http
Imports System.Text.Json.Nodes
Public Class Form1
    Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Using http = New HttpClient
            Dim url = "https://api-cloud.bitmart.com/account/v1/currencies"
            Dim json = JsonNode.Parse(Await http.GetStreamAsync(url))
            Dim naming As [String] = json("currency") '("naming ")

            RichTextBox1.Text = json("currency")
        End Using
    End Sub
End Class

but clicking the button doesn't populate anything. The richtextbox stays empty, while I want to get all the values ( es : "DFC, "$GM", "BBK" ecc)

I'm using .net6 but a framework.net solution would be appreciated. Thanks

Developer technologies VB
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.5K Reputation points
    2022-02-05T03:48:28.653+00:00

    Try something like this:

    Imports System.IO
    Imports System.Net.Http
    Imports System.Text.Json
    
    . . .
    
    Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
        RichTextBox1.Text = "Loading data..."
        RichTextBox1.Update()
    
        Using http = New HttpClient
            Dim url = "https://api-cloud.bitmart.com/account/v1/currencies"
            Dim s As Stream = Await http.GetStreamAsync(url)
            Dim r As Response = Await JsonSerializer.DeserializeAsync(Of Response)(s)
    
            RichTextBox1.Text = ""
    
            For Each c In r.data.currencies
                RichTextBox1.AppendText(c.currency & vbCrLf)
            Next
        End Using
    
    End Sub
    
    Class Response
        Public Property data As Data
    End Class
    
    Class Data
        Public Property currencies As Currency()
    End Class
    
    Class Currency
        Public Property currency As String
    End Class
    

    You can extend the Currency class to include more properties according to JSON.

    In case of .NET Framework, use NuGet Manager to add a reference to "System.Text.Json" package.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.