0

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

3
  • So, during debugging when you inspect json variable, does it have any content? Very off the top of my head, but fell like you're missing a step here. Think you need to do something like Dim response = await Http.GetStreamAsync, then work with the response.content Commented Feb 4, 2022 at 22:49
  • hey @Hursey, where I am supposed to try that code? between what? Thanks Commented Feb 4, 2022 at 23:05
  • Take a look at the supplied answer, I think should get you moving in the right direction Commented Feb 4, 2022 at 23:09

1 Answer 1

1

.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
Sign up to request clarification or add additional context in comments.

11 Comments

Hey @JayV thanks for your reply. i've just try your code but the output is just "DFA", it doesn't return anything else.
@878757 DFA is the last currency in the list. This tells me everything except the last line in your code is fine. Try something like RichTextBox1.Text += json("currency") to concatenate the currency list instead of replacing it each time
I've tried my code again now as you are stating and modify the last line. But i got the error: + operator is not define for string type and jsonNode
RichTextBox1.Text += jsonCurrency("currency").ToString + " "
So you are using your code now :). It is working ! Just a thing. If i want to get also the value "network", how can I do ? i've tried with: Dim jsonchain As JsonNode = jsonData("network") and For Each jsonCurrency As JsonNode In jsonCurrencies.AsArray() And jsonchain As Jsonnode In jsonchain.asarray() but obviously the loop is incorrect. Would it be possible? thanks a lot
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.