我有类似这样的方法来探测服务器以获取对象列表:
Public Class AllCustomers
Private Sub AllCustomers_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim request = WebRequest.CreateHttp("http://****************/api/customer")
request.Method = HttpMethod.Get.Method
Dim response As WebResponse = request.GetResponse()
Dim dataStream As Stream = response.GetResponseStream()
Dim reader As New StreamReader(dataStream)
Dim responseFromServer As String = reader.ReadToEnd()
End Sub
End Class
它返回以下字符串(作为示例):
[
{
"CustomerId": 1,
"FirstName": "Rodrigo",
"LastName": "Carvajal",
"Age": 26
},
{
"CustomerId": 2,
"FirstName": "Rodrigo",
"LastName": "Carvajal",
"Age": 26
},
{
"CustomerId": 3,
"FirstName": "Rodrigo",
"LastName": "Carvajal",
"Age": 26
}
]
这些属性实际上来自Customer对象。我想将此字符串转换为客户列表,然后在Windows窗体的表中将它们显示为行。如何将字符串转换为对象列表?
然后,在表中显示它们的理想方法是什么? DataGridView是一个不错的选择吗?我读到我也必须创建一个DataSet对象。还有其他选择吗?
谢谢!
答案 0 :(得分:1)
如何将字符串转换为对象列表?
您可以使用Json.net
Dim customers As List(Of Customer)
customers = JsonCovert.DeserializeObject(Of List(Of Customer))(responseFromServer)
然后,在表中显示它们的理想方法是什么?
将它们添加到网格可以绑定的数据源中。
Dim list As BindingList(Of Customer) = new BindingList(Of Customer)(customers)
myGrid.DataSource = list
或者您可以将列表添加为客户列表
myGrid.DataSource = customers