JSON数据如下所示。有时它会改变字段名称位置。如何将这些数据存储到Datatable中,并且必须在适当的列中。建议VB的任何解决方案。净
<string xmlns="http://tecogis.com/">[
{
"REPORT NUMBER":"LG534248650",
"REPORT CITY DATE":"June 22, 2022",
"DESCRIPTION":"LABORATORY GROWN DIAMOND",
"SHAPE AND CUT":"ROUND BRILLIANT",
"CARAT WEIGHT":"1.07 Carat",
"COLOR GRADE":"D",
"CLARITY GRADE":"SI 2",
"CUT GRADE":"VERY GOOD",
"POLISH":"EXCELLENT",
"SYMMETRY":"EXCELLENT",
"Measurements":"6.41 - 6.44 x 4.10 mm",
"Table Size":"57%",
"Crown Height":"15.5% - 35.4°",
"Pavilion Depth":"43.5% - 41.1°",
"Girdle Thickness":"MEDIUM TO SLIGHTLY THICK (FACETED)",
"Culet":"POINTED",
"Total Depth":"63.9%",
"FLUORESCENCE":"NONE",
"COMMENTS":"As Grown - No indication of post-growth treatmentrnThis Laboratory Grown Diamond was created by High Pressure High Temperature (HPHT) growth processrnType IIrn",
"Inscription(s)":"LABGROWN IGI LG534248650u003cbru003e",
"REPORT_SUF":"LEGAL",
"PDF_FLAG":"Y",
"REPORT1_PDF":"FDR534248650.pdf"
}
]</string>
这是您需要的缺失部分。请注意,这是我自己的项目,所以请根据您的情况进行相应调整。
在此之前,您只需要创建一个自定义类型的List,从JSON中填充它(参见Hursey的回答)并引用它,而不是从我的代码中引用BlogList。
这段代码将把列表中的数据传输到DataTable中,DataTable是DataGridView的数据源。
'Declare a DataTable:
Private BlogTable As New DataTable
'Through code, modify a DataGridView instance on your form and clear things:
dgvBlogs.Columns.Clear()
dgvBlogs.DataSource = Nothing
'Set up our columns in the DataTable:
BlogTable.Columns.Add("blogname")
BlogTable.Columns.Add("username")
BlogTable.Columns.Add("password")
dgvBlogs.AutoGenerateColumns = True
dgvBlogs.DataSource = BlogTable
'Put data from JSON into the DataSource:
BlogTable.Rows.Clear()
For Each item As BlogRecord In BlogList
Dim row As DataRow = BlogTable.NewRow()
row("blogname") = item.BlogName
row("username") = item.Username
row("password") = item.Password
BlogTable.Rows.Add(row)
Next
所以,从概念上讲,它是这样的:
JSON→(班级)名单->DataTable→DataGridView。数据源
您将JSON转换为您想要调用的任何自定义项类型的列表。然后从中填充一个DataTable。你要确保你的DataGridView。DataSource属性设置为DataTable。
这对我有用。唯一要添加的是仔细检查AutoGenerateColumns
,因为出于某种原因,在我的代码中,我首先将其设置为True,然后再设置为False。我敢肯定那只是我的实验,我从来没有清理过。