[英]Json String Into DataTable using VB.net
我已经咀嚼了两天,但无法使用我发现的样本来工作。 我真的很难解析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)
这似乎是一个错误(也许在VB中)。
对于我的测试,我使用了JsonTextReader(),但出现了相同的错误。 我没有尝试创建数据表。
请注意下面的代码(第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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.