使用 Visual Basic 将新项目添加到列表 json
Adding a new item to a list json using Visual Basic
所以我正在构建一个应用程序,我正在读取一个 JSON 文件,这样我就可以添加一个新的预订,但是在网上搜索后我没有找到任何方法来添加一个新的 dict
到 list
使用 visual basic
和 Newtonsoft.JSON
.
json 文件:
{"reservations": [{"time": "07:00 pm", "tableId": "1", "clientName": "Antonio Goncalves", "status": "pending"}]}
基本上我想在预订列表中添加一个新的值字典。
当前函数
Public Sub SetReservation(time As String, tableId As String, clientName As String, Optional status As String = "pending")
Dim reservationFile As String = File.ReadAllText(reservationJsonFile)
If Not String.IsNullOrEmpty(reservationFile) And Not String.IsNullOrWhiteSpace(reservationFile) Then
Dim reservationJson = Linq.JObject.Parse(reservationFile)
Dim newReservationObject = Linq.JObject.FromObject(New Dictionary(Of Object, Object) From {{"time", time}, {"tableId", tableId}, {"clientName", clientName}, {"status", status}})
Trace.WriteLine(newReservationObject)
End If
End Sub
创建代表您的 json 的 类 使 json 的使用变得更加简单。查看为表示您的 json 数据而创建的 类 以及如何在您的子例程中使用它。
'Root object representing the "reservations" key in the json file
Public Class ReservationData
Public Property Reservations As List(Of Reservation)
End Class
'Properties match the expected keys in the json file
Public Class Reservation
Public Property Time As String
Public Property TableId As String
Public Property ClientName As String
Public Property Status As String
End Class
作为补充说明包含的评论。
Public Sub SetReservation(time As String, tableId As String, clientName As String, Optional status As String = "pending")
Dim reservationFile As String = File.ReadAllText(reservationJsonFile)
If Not String.IsNullOrWhiteSpace(reservationFile) Then
'Convert the json string to a ReservationData object
Dim reservationData = JsonConvert.DeserializeObject(Of ReservationData)(reservationFile)
'Create a new Reservation
Dim newReservation = New Reservation With {.Time = time, .TableId = tableId, .ClientName = clientName, .Status = status}
'Access the Reservations list from reservationData and Add the new Reservation
reservationData.Reservations.Add(newReservation)
'Overwrite the file with the updated reservationData
File.WriteAllText(reservationJsonFile, JsonConvert.SerializeObject(reservationData))
End If
End Sub
所以我正在构建一个应用程序,我正在读取一个 JSON 文件,这样我就可以添加一个新的预订,但是在网上搜索后我没有找到任何方法来添加一个新的 dict
到 list
使用 visual basic
和 Newtonsoft.JSON
.
json 文件:
{"reservations": [{"time": "07:00 pm", "tableId": "1", "clientName": "Antonio Goncalves", "status": "pending"}]}
基本上我想在预订列表中添加一个新的值字典。
当前函数
Public Sub SetReservation(time As String, tableId As String, clientName As String, Optional status As String = "pending")
Dim reservationFile As String = File.ReadAllText(reservationJsonFile)
If Not String.IsNullOrEmpty(reservationFile) And Not String.IsNullOrWhiteSpace(reservationFile) Then
Dim reservationJson = Linq.JObject.Parse(reservationFile)
Dim newReservationObject = Linq.JObject.FromObject(New Dictionary(Of Object, Object) From {{"time", time}, {"tableId", tableId}, {"clientName", clientName}, {"status", status}})
Trace.WriteLine(newReservationObject)
End If
End Sub
创建代表您的 json 的 类 使 json 的使用变得更加简单。查看为表示您的 json 数据而创建的 类 以及如何在您的子例程中使用它。
'Root object representing the "reservations" key in the json file
Public Class ReservationData
Public Property Reservations As List(Of Reservation)
End Class
'Properties match the expected keys in the json file
Public Class Reservation
Public Property Time As String
Public Property TableId As String
Public Property ClientName As String
Public Property Status As String
End Class
作为补充说明包含的评论。
Public Sub SetReservation(time As String, tableId As String, clientName As String, Optional status As String = "pending")
Dim reservationFile As String = File.ReadAllText(reservationJsonFile)
If Not String.IsNullOrWhiteSpace(reservationFile) Then
'Convert the json string to a ReservationData object
Dim reservationData = JsonConvert.DeserializeObject(Of ReservationData)(reservationFile)
'Create a new Reservation
Dim newReservation = New Reservation With {.Time = time, .TableId = tableId, .ClientName = clientName, .Status = status}
'Access the Reservations list from reservationData and Add the new Reservation
reservationData.Reservations.Add(newReservation)
'Overwrite the file with the updated reservationData
File.WriteAllText(reservationJsonFile, JsonConvert.SerializeObject(reservationData))
End If
End Sub