Home > other >  How to get all the values from a JSON array
How to get all the values from a JSON array

Time:02-06

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") 

            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

CodePudding user response:

.Net Core answer

Take a look at my suggestion below. I can see that you are missing several steps in parsing the Json data as well as not getting the result of the GetStreamAsync operation.

Dim streamData As Stream = Nothing
Using http As HttpClient = New HttpClient
    Dim url As String = "https://api-cloud.bitmart.com/account/v1/currencies"

    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("data")
Dim jsonCurrencies As JsonNode = jsonData("currencies")

Dim c As String = String.Empty
Dim n As String = String.Empty
For Each jsonCurrency As JsonNode In jsonCurrencies.AsArray()
    c  = jsonCurrency("currency").ToString   " "
    n  = jsonCurrency("network").ToString   " "
Next


Debug.WriteLine(c)
Debug.WriteLine(n)

This will output all of the currencies downloaded from: https://api-cloud.bitmart.com/account/v1/currencies

My test program doesn't work well with Async/Await operations so I removed the call to Await. I believe you can put it back in without issue.

This is a .Net Core answer, I believe for .Net Framework you will need to use a different Json parser, such as Newtonsoft.

.Net Framework 4.8 answer

This version makes use of Newtonsoft Json parser and is slightly different to the .Net Core version as it make use of Async/Await

Private Async Sub DownloadData()
    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   " "
        n  = jsonCurrency("network").ToString   " "
    Next

    Debug.WriteLine(c)
    Debug.WriteLine(n)
End Sub
  •  Tags:  
  • Related