asp.net - 将 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
推荐阅读
- office-ui-fabric-react - ICommandBarItemProps 不支持焦点方法
- julia - 使用 Julia 实现递归函数返回的问题
- javascript - “gpu.js”如何操纵 GPU 线程?
- spring-boot - Hello World with Camel 3.4 + Spring boot 2
- vue.js - 如何在 nginx + vue.js 中显示自定义 500 页面?
- git - gitconfig shell-command 别名从顶层而不是当前目录运行
- string - 用于选择具有最新日期且不带子字符串的文件名的 Bash 脚本
- mysql - NodeJs 同步 MYSQL 查询和 Foreach
- node.js - 在控制器的“续集”中使用模型
- c# - 属性更改未在任务内触发