将 JSON 响应转换为 gridview
Convert JSON response to gridview
我有来自 API 的以下 JSON 响应,我需要在 Gridview 中显示它:
[
{
"count": 271,
"headings": [
"Application",
"Host",
"os_type",
"os_version",
"model",
"vendor",
"virtual"
],
"kind": "BSI",
"next": "NextROW",
"next_offset": 100,
"offset": 0,
"results": [
[
"Customer Documents Archive System",
"Microsoft Network LoadBalancer mainserver running on 10.75.0.99",
null,
null,
null,
null,
null
],
[
"Customer Documents Archive System Group",
"Microsoft Network LoadBalancer server1 running on 10.128.2.143",
null,
null,
null,
null,
null
],
[
"Customer Documents Archive System Group",
"Microsoft Network LoadBalancer server2 running on 10.21.5.100",
null,
null,
null,
null,
null
],
[
"KIOSK",
"EastServer",
null,
null,
null,
null,
null
],
[
"Hotline",
"EastServer",
null,
null,
null,
null,
null
],
[
"ProjectWise",
"NorthServer",
"Windows",
"Server 2012 R2",
"VMware Virtual Platform",
"VMware, Inc.",
true
],
[
"SMS System",
"CentralServer",
"Windows",
"Server 2016",
"VMware Virtual Platform",
"VMware, Inc.",
true
]
],
"results_id": "QnVzaW5lc3N"
}
]
我正在使用以下代码:
Dim steamreader As StreamReader = New StreamReader(stream)
Dim strdata As String = steamreader.ReadToEnd
Dim rs As List(Of Root) = JsonConvert.DeserializeObject(Of List(Of Root))(strdata)
GridView1.DataSource = rs
GridView1.DataBind()
Public Class Root
Public Property count As Integer
Public Property headings As List(Of String)
Public Property kind As String
Public Property [next] As String
Public Property next_offset As Integer
Public Property offset As Integer
Public Property results As List(Of List(Of Object))
Public Property results_id As String
End Class
网格视图HTML:
<asp:GridView ID = "GridView1" runat = "server">
JSON 数据所需的输出:
以上代码无效。谁能指导我如何使用 JSON 响应中的“标题”作为 Gridview 列 headers 以及“结果”中的相应数据作为行在 gridview 中显示来自 JSON 的数据列的数据?
你所拥有的应该有用。我会仔细检查什么 strData(在调试中执行 debug.print(strData) 检查(或根据您的设置输出 window)。
我刚刚放入一个文本框,网格视图。有了这个:
<asp:TextBox ID="TextBox1" runat="server" Height="125px"
TextMode="MultiLine" Width="285px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" Width="111px" />
<br />
<br />
<div style="width:40%">
<asp:GridView ID="GridView1" runat="server" CssClass="table table-hover"></asp:GridView>
</div>
然后我将您的 json 剪切并粘贴到文本框中
我得到了这个结果:
所以,这应该可行。我将 class 创建为一个单独的模块 (class),然后将数据粘贴到您的 class 中。
我只能猜测您发布的 json 不是您从该流 reader 获得的内容。
我上面的按钮代码是这样的:
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim strJSON As String = TextBox1.Text
Dim rs As List(Of RootTest)
rs = JsonConvert.DeserializeObject(Of List(Of RootTest))(strJSON)
GridView1.DataSource = rs
GridView1.DataBind()
End Sub
而 class 是:
Public Class RootTest
Public Property count As Integer
Public Property headings As List(Of String)
Public Property kind As String
Public Property [next] As String
Public Property next_offset As Integer
Public Property offset As Integer
Public Property results As List(Of List(Of Object))
Public Property results_id As String
End Class
从数据的排列方式来看,您需要将每组结果的数据提取到 DataTable 中,并将这些数据表添加到 DataSet 中。
我使用了 Windows 表单,但您应该能够轻松地调整它以使用 ASP.NET GridView:
Imports System.IO
Imports Newtonsoft.Json
Public Class Form1
Public Class Root
Public Property count As Integer
Public Property headings As List(Of String)
Public Property kind As String
Public Property [next] As String
Public Property next_offset As Integer
Public Property offset As Integer
Public Property results As List(Of List(Of Object))
Public Property results_id As String
End Class
Sub ShowData(f As String)
Dim allResults As List(Of Root)
Using sr As StreamReader = New StreamReader(f)
Dim dataAsJson As String = sr.ReadToEnd()
allResults = JsonConvert.DeserializeObject(Of List(Of Root))(dataAsJson)
End Using
Dim ds As New DataSet()
For Each resultSet In allResults
Dim dt As New DataTable(resultSet.results_id)
For Each heading In resultSet.headings
dt.Columns.Add(New DataColumn With {.ColumnName = heading, .DataType = Type.GetType("System.String")})
Next
For i = 0 To resultSet.results.Count - 1
Dim nr = dt.NewRow()
For j = 0 To resultSet.results(i).Count - 1
Dim thisItem = resultSet.results(i).Item(j)
nr(j) = If(thisItem Is Nothing, "-", CStr(thisItem))
Next
dt.Rows.Add(nr)
Next
ds.Tables.Add(dt)
Next
DataGridView1.DataSource = ds.Tables(0)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim j = "C:\temp\SO68055446.json"
ShowData(j)
End Sub
End Class
我有来自 API 的以下 JSON 响应,我需要在 Gridview 中显示它:
[
{
"count": 271,
"headings": [
"Application",
"Host",
"os_type",
"os_version",
"model",
"vendor",
"virtual"
],
"kind": "BSI",
"next": "NextROW",
"next_offset": 100,
"offset": 0,
"results": [
[
"Customer Documents Archive System",
"Microsoft Network LoadBalancer mainserver running on 10.75.0.99",
null,
null,
null,
null,
null
],
[
"Customer Documents Archive System Group",
"Microsoft Network LoadBalancer server1 running on 10.128.2.143",
null,
null,
null,
null,
null
],
[
"Customer Documents Archive System Group",
"Microsoft Network LoadBalancer server2 running on 10.21.5.100",
null,
null,
null,
null,
null
],
[
"KIOSK",
"EastServer",
null,
null,
null,
null,
null
],
[
"Hotline",
"EastServer",
null,
null,
null,
null,
null
],
[
"ProjectWise",
"NorthServer",
"Windows",
"Server 2012 R2",
"VMware Virtual Platform",
"VMware, Inc.",
true
],
[
"SMS System",
"CentralServer",
"Windows",
"Server 2016",
"VMware Virtual Platform",
"VMware, Inc.",
true
]
],
"results_id": "QnVzaW5lc3N"
}
]
我正在使用以下代码:
Dim steamreader As StreamReader = New StreamReader(stream)
Dim strdata As String = steamreader.ReadToEnd
Dim rs As List(Of Root) = JsonConvert.DeserializeObject(Of List(Of Root))(strdata)
GridView1.DataSource = rs
GridView1.DataBind()
Public Class Root
Public Property count As Integer
Public Property headings As List(Of String)
Public Property kind As String
Public Property [next] As String
Public Property next_offset As Integer
Public Property offset As Integer
Public Property results As List(Of List(Of Object))
Public Property results_id As String
End Class
网格视图HTML:
<asp:GridView ID = "GridView1" runat = "server">
JSON 数据所需的输出:
以上代码无效。谁能指导我如何使用 JSON 响应中的“标题”作为 Gridview 列 headers 以及“结果”中的相应数据作为行在 gridview 中显示来自 JSON 的数据列的数据?
你所拥有的应该有用。我会仔细检查什么 strData(在调试中执行 debug.print(strData) 检查(或根据您的设置输出 window)。
我刚刚放入一个文本框,网格视图。有了这个:
<asp:TextBox ID="TextBox1" runat="server" Height="125px"
TextMode="MultiLine" Width="285px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" Width="111px" />
<br />
<br />
<div style="width:40%">
<asp:GridView ID="GridView1" runat="server" CssClass="table table-hover"></asp:GridView>
</div>
然后我将您的 json 剪切并粘贴到文本框中
我得到了这个结果:
所以,这应该可行。我将 class 创建为一个单独的模块 (class),然后将数据粘贴到您的 class 中。
我只能猜测您发布的 json 不是您从该流 reader 获得的内容。
我上面的按钮代码是这样的:
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim strJSON As String = TextBox1.Text
Dim rs As List(Of RootTest)
rs = JsonConvert.DeserializeObject(Of List(Of RootTest))(strJSON)
GridView1.DataSource = rs
GridView1.DataBind()
End Sub
而 class 是:
Public Class RootTest
Public Property count As Integer
Public Property headings As List(Of String)
Public Property kind As String
Public Property [next] As String
Public Property next_offset As Integer
Public Property offset As Integer
Public Property results As List(Of List(Of Object))
Public Property results_id As String
End Class
从数据的排列方式来看,您需要将每组结果的数据提取到 DataTable 中,并将这些数据表添加到 DataSet 中。
我使用了 Windows 表单,但您应该能够轻松地调整它以使用 ASP.NET GridView:
Imports System.IO
Imports Newtonsoft.Json
Public Class Form1
Public Class Root
Public Property count As Integer
Public Property headings As List(Of String)
Public Property kind As String
Public Property [next] As String
Public Property next_offset As Integer
Public Property offset As Integer
Public Property results As List(Of List(Of Object))
Public Property results_id As String
End Class
Sub ShowData(f As String)
Dim allResults As List(Of Root)
Using sr As StreamReader = New StreamReader(f)
Dim dataAsJson As String = sr.ReadToEnd()
allResults = JsonConvert.DeserializeObject(Of List(Of Root))(dataAsJson)
End Using
Dim ds As New DataSet()
For Each resultSet In allResults
Dim dt As New DataTable(resultSet.results_id)
For Each heading In resultSet.headings
dt.Columns.Add(New DataColumn With {.ColumnName = heading, .DataType = Type.GetType("System.String")})
Next
For i = 0 To resultSet.results.Count - 1
Dim nr = dt.NewRow()
For j = 0 To resultSet.results(i).Count - 1
Dim thisItem = resultSet.results(i).Item(j)
nr(j) = If(thisItem Is Nothing, "-", CStr(thisItem))
Next
dt.Rows.Add(nr)
Next
ds.Tables.Add(dt)
Next
DataGridView1.DataSource = ds.Tables(0)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim j = "C:\temp\SO68055446.json"
ShowData(j)
End Sub
End Class