使用VB.net将Json String转换为DataTable

时间:2013-04-04 12:42:14

标签: vb.net json

我一直在咀嚼这一天两天,并且使用我找到的样品无法使用它。我真的很难用json解析。

所以我需要做的是将此字符串放入数据表中。我能够达到我拥有字符串的程度,我只需要解析它。

{"total": 35799, "results": [{"publisher": "bamamatch.com", "first_seen": "2011-08-01", "times_seen": 1598, "monthly_uniques": null, "last_seen": "2013-04-02"}, {"publisher": "catholicdatingforfree.com", "first_seen": "2011-08-01", "times_seen": 1554, "monthly_uniques": null, "last_seen": "2013-04-02"}], "page_size": 100, "offset": 0}

你们有没有向我展示一种使用VB.net将其解析为数据表的明确方法?

更新

        Dim url As String
        Dim request As HttpWebRequest
        Dim response As HttpWebResponse = Nothing
        Dim reader As StreamReader

        url = "http://api.mixrank.com/v2/json/d73f10e7b22fbc69b79f0e0074913c14/advertisers/" & LCase(txtKeywords.Text) & "/gdn/publishers?page_size=100"

        request = DirectCast(WebRequest.Create(url), HttpWebRequest)
        response = DirectCast(request.GetResponse(), HttpWebResponse)
        reader = New StreamReader(response.GetResponseStream())

        Dim myDataTable As DataTable = DirectCast(JsonConvert.DeserializeObject(reader.ReadToEnd, (GetType(DataTable))), DataTable)

1 个答案:

答案 0 :(得分:0)

这似乎是一个错误(可能在VB某处。)

对于我的测试,我使用了JsonTextReader(),但出现了同样的错误。我没有尝试创建DataTable。

请注意下面的代码(第3行),它会从测试字符串中删除最后一个字符。如果没有此调整,则会出现“附加文本”错误。

另外 - 在OP中我需要将提供的JSON包装在[ ]中以使其能够使用此代码。

    Try
        Dim s As String = TextBox1.Text
        s = s.Remove(s.Length - 1)
        Dim txt As New System.IO.StringReader(s)
        txt.Read()
        Dim reader As New JsonTextReader(txt)
        Do While reader.Read
            If reader.Value IsNot Nothing Then Debug.Print(reader.Depth & ": " & reader.TokenType & ": " & reader.Path & ": " & reader.Value)
        Loop
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try