How can I show my JSON results in a Textbox instead of writing to the Console?

Viewed 140

I'm running into a little problem that I haven't found a way to to solve.
I haven't found a forum where this specific problem is addressed, I really hope to find some help.

Here is my code:

Imports System.IO
Imports System.Net
Imports Newtonsoft.Json.Linq

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim request As HttpWebRequest
        Dim response As HttpWebResponse = Nothing
        Dim reader As StreamReader

        request = DirectCast(WebRequest.Create("https://pastebin.com/raw/dWjmfW8N"), HttpWebRequest)

        response = DirectCast(request.GetResponse(), HttpWebResponse)
        reader = New StreamReader(response.GetResponseStream())

        Dim jsontxt As String
        jsontxt = reader.ReadToEnd()
        Dim myJObject = JObject.Parse(jsontxt)
        For Each match In myJObject("matches")
            Console.WriteLine(match("http")("host").ToString)
        Next

    End Sub

End Class

Here is the output:

223.16.205.13
190.74.163.58
71.7.168.29
117.146.53.244
31.170.146.28
118.36.122.169
123.7.117.78
113.61.154.182
36.48.37.191
113.253.179.234
124.13.29.41
180.122.74.183
121.157.114.93
39.78.35.216
176.82.1.100
201.143.142.75
222.117.29.229
89.228.209.185
59.153.89.245
148.170.162.37
112.160.243.23
62.101.254.177
190.141.161.149
121.132.177.79
79.165.124.174
118.39.91.43
220.83.82.58
220.161.101.195
190.218.188.86
123.241.174.77
219.71.218.113
81.198.205.2
1.64.205.1
190.204.66.180
203.163.241.36
36.34.148.33
221.124.127.89
115.29.210.231
39.121.63.13
178.160.38.191
117.146.55.217
149.91.99.49
220.93.231.104
49.245.71.40
211.44.70.107
37.119.247.51
222.101.54.200
178.163.102.223
119.198.145.129
188.26.240.141
115.29.233.160
190.164.29.145
94.133.185.144
181.37.196.134
116.88.213.9
115.2.194.11
1.226.12.161
178.63.73.210
49.149.194.242
14.32.29.251
59.0.191.68
58.122.168.43
142.129.230.137
105.145.89.51
201.243.97.65
175.37.162.102
186.88.141.126
105.148.43.100
60.179.173.21
69.115.51.207
90.171.193.132
14.64.76.165
121.127.95.80
175.211.168.48
99.240.74.72
58.153.174.2
119.77.168.142
121.170.47.232
58.243.20.124
199.247.243.234
47.111.76.211
93.72.213.251
218.32.44.73
220.83.90.204
119.158.102.20
95.109.55.204
106.5.19.223
190.199.215.69
190.218.57.249
36.102.72.163
219.78.162.215
177.199.151.96
196.93.125.34
211.58.150.166
180.131.163.40
93.156.97.81
159.89.22.81
130.0.55.156
186.93.202.111
195.252.44.173

What I want to do is to transfer that console output to my Textbox1.Text. Can anyone please show me a way to solve this?

3 Answers

There's no need to transfer anything. If you want the data in a TextBox then put it in a TextBox. You can then output the same data using Console.WriteLine or Debug.WriteLine. You can use a loop:

Dim hosts As New List(Of String)

For Each match In myJObject("matches")
    hosts.Add(match("http")("host").ToString())
Next

Dim text = String.Join(Environment.NewLine, hosts)

myTextBox.Text = text
Console.WriteLine(text)

You could also use LINQ:

Dim text = String.Join(Environment.NewLine, myJObject("matches").Select(Function(match) match("http")("host").ToString()))

myTextBox.Text = text
Console.WriteLine(text)

A somewhat simplified method, using WebClient's DownloadStringTaskAsync to download the JSON.
You don't need special treatment here, strings that represent IpAddresses are just numbers and dots and the source encoding is probably UTF8.

After that, just parse the JSON and Select() the property values you care about, transform the resulting Enumerable(Of JToken) to an array of strings and set the array as the source of a TextBox.Lines property.

You can store the lines collection for any other use, in case it's needed.

Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Using client As New WebClient()
        Dim json = Await client.DownloadStringTaskAsync([The URL])
        Dim parsed = JObject.Parse(json)
        Dim lines = parsed("matches").
            Where(Function(jt) jt("http") IsNot Nothing).
            Select(Function(jt) jt("http")("host").ToString()).ToArray()
        TextBox1.Lines = lines
    End Using
End Sub

Alternative approach to display collection of things in Winforms are ListView, DataGridView or other collection controls depends on desired usage.

Add ListView control in designer and next code will fill it with received values.

Shared ReadOnly client As HttpClient = New HttpClient()

Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim response As HttpResponseMessage = 
        Await client.GetAsync("https://pastebin.com/raw/dWjmfW8N")
    response.EnsureSuccessStatusCode()

    Dim jsonBody As String = Await response.Content.ReadAsStringAsync()
    Dim myJObject = JObject.Parse(jsonBody)

    ListView1.Items.Clear()
    For Each match In myJObject("matches")
        ListView1.Items.Add(match("http")("host").ToString)
    Next
End Sub
Related