从DataGridView创建Json属性

ubby3x7f  于 2023-10-21  发布在  其他
关注(0)|答案(2)|浏览(102)

我不知道该怎么解释,但事情是这样的:
我正在尝试从DataGridView创建JSON文件的属性。我使用newtonsoft来处理JSON序列化。
从本质上讲,用户将把他们的值添加到两列中,这些值将被转换为属性。假设用户在Column1中输入值10,20,30,然后在Column2中输入“A”“B””C”,那么结果JSON应该是:

prompt_map: {
"10": "A"
"20": "B"
"30": "C"
}

它应该根据用户创建了多少行而继续运行。
我想我会分享我的代码,但我不确定它是否会有帮助,因为我甚至不知道如何处理这一点。
这些值将作为嵌套放入prompt_map字符串中

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      
    Dim myJson As JsonStructure
    myJson = New JsonStructure
    myJson.prompt_map

    Dim myString As String
    myString = JsonConvert.SerializeObject(myJson, Formatting.Indented)
    MsgBox(myString) 'debugging

End Sub

Public Class JsonStructure
    Public prompt_map As Frames
End Class

Public Class Frames
    'CODE GOES HERE?
End Class

这是我用来从DataGridView中提取数据的方法:

Dim colFrames As String
For i = 0 To DataGridView1.RowCount - 1
    If Not (DataGridView1.Rows(i).Cells("Column1").Value = 0) Then
        colFrames = colFrames + vbNewLine + (DataGridView1.Rows(i).Cells("Column1").Value)
    End If
Next

Dim colDesc As String
For i = 0 To DataGridView1.RowCount - 1
    If Not (DataGridView1.Rows(i).Cells("Column2").Value = 0) Then
        colDesc = colDesc + vbNewLine + (DataGridView1.Rows(i).Cells("Column2").Value)
    End If
Next

我不确定这有多有用,只是展示我的尝试。
任何帮助将不胜感激。我已经忙了一整天了。

0ve6wy6x

0ve6wy6x1#

一种选择是通过循环将行处理为Dictionary(Of String, String),然后使用Newtonsoft序列化字典。
但是对于这么多的工作,你最好记住JSON只是一个字符串,去掉中间件,直接通过StringBuilder或类似的东西构建结果。此外,Dictionary方法不允许重复的键,而dubiously-legal方法可能允许用户输入数据。

' Expect a datagridview with exactly two columns
Public Function CreateJSON(source As DataGridView) As String
    If source Is Nothing Then Return "prompt_map: {}"
    Dim result As New StringBuilder("prompt_map: {" & vbCrLf)

    Dim delimiter As String = ""
    For i As Integer = 0 To source.RowCount - 1
        result.Append($"{delimiter}""{FormatCell(source.Rows(i).Cells(0))}"":""{FormatCell(source.Rows(i).Cells(1))}""")
        delimter = $",{vbCrLf}"
    Next
    result.Append($"{vbCrLf}}")
    return result.ToString()
End Function

Private Function FormatCell(cell As DataGridViewCell) As String
    If cell Is Nothing OrElse cell.Value Is Nothing Then Return ""
    Return cell.Value.ToString().Replace("""", "\""")
End Function
mwecs4sa

mwecs4sa2#

您可以使用DataTable作为您的DataGridView.DataSource,并使用Dictionary(Of String, String)保存序列化数据。

Imports Newtonsoft.Json.Linq

Public Class Form1
  'Add a DataGridView and a Button to the form

  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    Dim gridData = New DataTable("SettingsData")
    gridData.Columns.Add(New DataColumn("Column1", GetType(String)))
    gridData.Columns.Add(New DataColumn("Column2", GetType(String)))
    DataGridView1.DataSource = gridData
  End Sub

  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim gridData = DirectCast(DataGridView1.DataSource, DataTable)
    Dim output = New OutputClass
    For rowIndex As Integer = 0 To gridData.Rows.Count - 1
      Dim row = gridData.Rows(rowIndex)
      Dim col1Value = row(0).ToString
      Dim col2Value = row(1).ToString
      output.prompt_map.Add(col1Value, col2Value)
    Next rowIndex
    Dim outputJson = Newtonsoft.Json.JsonConvert.SerializeObject(output)
    System.IO.File.WriteAllText("C:\Junk\junk.json", outputJson)
  End Sub

  Class OutputClass
    Public Property prompt_map As New Dictionary(Of String, String)
  End Class
End Class

输出量:

{
  "prompt_map": {
    "10": "A",
    "20": "B",
    "30": "C"
  }
}

相关问题