简体   繁体   中英

Json String Into DataTable using VB.net

I've been chewing on this one for two days and can't get it to work using the samples I have found. I really struggle with json parsing.

So what I need to do is get this string into a datatable. I'm able to get to the point where I have the string, I just need it parsed.

{"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}

Could you guys show me a clear way to parse this into a datatable using VB.net?

UPDATE

        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)

This appears to be a bug (perhaps in VB somewhere.)

For my test I used the JsonTextReader() but the same error appears. I didn't try creating a DataTable.

Note the code below (line 3) that removes the last character from the test string. Without this tweak the "Additional text" error appears.

Also - in the OP I needed to wrap the supplied JSON in [ ] to get it to work with this code.

    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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM