【问题标题】:How can I show my JSON results in a Textbox instead of writing to the Console?如何在文本框中显示我的 JSON 结果而不是写入控制台?
【发布时间】:2021-01-04 05:05:22
【问题描述】:

我遇到了一个小问题,我还没有找到解决方法。
我还没有找到解决这个特定问题的论坛,我真的希望能找到一些帮助。

这是我的代码:

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

这是输出:

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

我想做的是将该控制台输出传输到我的Textbox1.Text。谁能告诉我解决这个问题的方法吗?

【问题讨论】:

    标签: json vb.net winforms json.net


    【解决方案1】:

    一种稍微简化的方法,使用 WebClient 的 DownloadStringTaskAsync 下载 JSON。
    这里不需要特殊处理,代表 IpAddresses 的字符串只是数字和点,源编码可能是 UTF8。

    之后,只需解析 JSON 和 Select() 你关心的属性值,将生成的 Enumerable(Of JToken) 转换为字符串数组并将该数组设置为 TextBox.Lines 属性。

    您可以存储 lines 集合以供任何其他用途,以备不时之需。

    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
    

    【讨论】:

      【解决方案2】:

      没有必要转移任何东西。如果您想要 TextBox 中的数据,请将其放入 TextBox 中。然后,您可以使用Console.WriteLineDebug.WriteLine 输出相同的数据。您可以使用循环:

      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)
      

      你也可以使用 LINQ:

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

      【讨论】:

        【解决方案3】:

        在 Winforms 中显示事物集合的替代方法是 ListViewDataGridView 或其他集合控件,具体取决于所需的用法。

        在设计器中添加ListView 控件,接下来的代码将用接收到的值填充它。

        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
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-01-10
          • 2018-07-13
          • 1970-01-01
          • 1970-01-01
          • 2020-09-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多