将 JSON 响应转换为 gridview

沙希德

我需要在 Gridview 中显示来自 API 的以下 JSON 响应:

[
    {
        "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 列标题和“结果”中的相应数据作为列的行数据在 gridview 中显示来自 JSON 的数据?

安德鲁·莫顿

从数据的排列方式来看,您需要将每组结果的数据提取到 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

来自 JSON 的结果项

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章