因此,我最近开始学习Visual Basic,并正在测试解析HTML数据的乐趣。当我进入一些JSON时,我下载了牛顿软件包并开始了解它的工作原理。我开始只是试图获取任何用户的Instagram页面的URL,但出现了一个我似乎无法解决的错误,而且我刚刚接触VB,我认为最好是寻求一些帮助,而不是困惑我的思维。Visual Basic Json.net Newtonsoft.Json.JsonReaderException错误
下面是代码:
Imports HtmlAgilityPack
Imports Newtonsoft.Json
Module Module1
Sub Main()
Dim user As String = Console.ReadLine()
Dim html = "https://www.instagram.com/" + user
Console.WriteLine(html)
Dim web As New HtmlWeb()
Dim htmlDoc = web.Load(html)
For Each node As HtmlNode In htmlDoc.DocumentNode.SelectNodes("//script[@type='text/javascript']")
If node.InnerHtml.Contains("profile_pic_url_hd") Then 'Makes sure the correct javascript code is used.
Dim json = node.InnerHtml.Substring(21, node.InnerHtml.Length - 21) 'Deletes the non Json code in the javascript.
Dim m As User = JsonConvert.DeserializeObject(Of User)(json) 'Error is here
Dim picture As String = m.profile_pic_url_hd
Console.WriteLine(picture)
Console.ReadLine()
Else
Console.WriteLine("Could not find correct code! Possibly because the username doesn't exist")
End If
Next
Console.WriteLine()
End Sub
Public Class User
Public Property biography As String
Public Property blocked_by_viewer As Boolean
Public Property country_block As Boolean
Public Property external_url As Object
Public Property external_url_linkshimmed As Object
Public Property followed_by As Integer
Public Property followed_by_viewer As Boolean
Public Property follows As Integer
Public Property follows_viewer As Boolean
Public Property full_name As String
Public Property has_blocked_viewer As Boolean
Public Property has_requested_viewer As Boolean
Public Property id As String
Public Property is_private As Boolean
Public Property is_verified As Boolean
Public Property profile_pic_url As String
Public Property profile_pic_url_hd As String
Public Property requested_by_viewer As Boolean
Public Property username As String
Public Property connected_fb_page As Object
Public Property media As Object
End Class
End Module
所以我就在这行错误:
Dim m As User = JsonConvert.DeserializeObject(Of User)(json)
说:Newtonsoft.Json.JsonReaderException:“附加文本遇到结束后读取JSON内容: ;。路径“,第1行,位置3220。 位置号码总是变化。但我不确定为什么会发生这种情况。
任何帮助我感谢!
编辑: JSON的是各位的Instagram帐户不同,但作为一个例子这里是国际足联的JSON: https://pastebin.com/J3U0uz4S
这将有助于张贴JSON - 也'作为Object'看起来很可疑。这通常意味着一个没有在JSON中表示的类型。 – Plutonix