vb.net 2010阅读json url并填充datagridview

时间:2015-10-11 08:35:49

标签: json vb.net datagridview

我是.net场景的完全新手。我有一些PHP的经验,但在vb.net 2010中绝对没有。

以下是我正在尝试阅读并填充数据网格视图的代码。

    Imports System.Net
    Imports System.IO
    Imports Newtonsoft.Json
    Imports Newtonsoft.Json.Linq
Public Class Form1


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click



        Dim request As HttpWebRequest
        Dim response As HttpWebResponse = Nothing
        Dim reader As StreamReader
        Dim url As String
        url = base_url & "get_report"
        request = DirectCast(WebRequest.Create(url), HttpWebRequest)

        response = DirectCast(request.GetResponse(), HttpWebResponse)
        reader = New StreamReader(response.GetResponseStream())

        Dim s As String
        s = reader.ReadToEnd
        MessageBox.Show(s)
        Dim res As results = JsonConvert.DeserializeObject(Of results)(s)


        DataGridView1.DataSource = res
    End Sub
End Class

Public Class results
    Public tracker As Integer
    Public dinaya As Date
    Public total As Double
    Public cardname As String
    Public sourcenum As Integer
End Class

我的问题是我得到了这个构建错误,我不知道调试。

无法将当前JSON数组(例如[1,2,3])反序列化为类型' WindowsApplication1.results'因为类型需要JSON对象(例如{" name":" value"})才能正确反序列化。 要修复此错误,请将JSON更改为JSON对象(例如{" name":" value"})或将反序列化类型更改为实现集合接口的数组或类型(例如ICollection,IList)就像可以从JSON数组反序列化的List。 JsonArrayAttribute也可以添加到类型中以强制它从JSON数组反序列化。 路径'',第1行,第1位。

我只是不知道该怎么做。 消息框显示json结果。 任何帮助是极大的赞赏。

NET EDIT

Imports System.Net
Imports System.IO
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Public Class Form1


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim request As HttpWebRequest
        Dim response As HttpWebResponse = Nothing
        Dim reader As StreamReader
        Dim url As String
        url = base_url & "get_report"
        request = DirectCast(WebRequest.Create(url), HttpWebRequest)

        response = DirectCast(request.GetResponse(), HttpWebResponse)
        reader = New StreamReader(response.GetResponseStream())

        Dim s As String
        s = reader.ReadToEnd
        MessageBox.Show(s) 'valid json output here
        Dim res As transactions = JsonConvert.DeserializeObject(Of transactions)(s)
        MessageBox.Show(res.cardname) 'pops up the proper field

    End Sub
End Class

Public Class transactions
    Public tracker As Integer
    Public dinaya As Date
    Public total As Double
    Public cardname As String
    Public sourcenum As Integer
End Class

我的问题是如何阅读以下json数据。

[{"tracker":"1","dinaya":"2015-01-01","total":"123","cardname":"Dileep","sourcenum":"1234444"},{"tracker":"12","dinaya":"2015-01-22","total":"1244443","cardname":"Diafasdfleep","sourcenum":"123342344444"}]

当json包含多个数组元素时,我的代码不起作用。任何循环至少的方法?

0 个答案:

没有答案