首先,我想说我不知道自己在做什么。我正在尝试从Twitch.tv读取json数据。我当前使用的URL看起来像这样:https : //api.twitch.tv/kraken/channels/seeingblue/follows
在我的程序内部,我设法对数据进行反序列化,并使数据看起来整洁易读。我在存储数据时遇到麻烦,这是一种有组织的方式。我想做的是定期轮询URL,并检查是否有任何更改。
我能够使该示例正常工作,但无法修改代码以使其满足我的需要。vb.net json.net解析结果
这是我目前拥有的:
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Try
Dim json As String = New WebClient().DownloadString("https://api.twitch.tv/kraken/channels/seeingblue/follows?limit=1&offset=0")
Dim root As JObject = JObject.Parse(json)
Dim stream As JToken = root("user")
Dim game As String = stream("name").ToString()
'Dim viewers As String = stream("_links").ToString()
MsgBox(game)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.OkOnly, "Error")
End Try
End Sub
但是我得到一个关于 Object reference not set to an instance of an object.
或者如果我尝试
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Try
Dim json As String = New WebClient().DownloadString("https://api.twitch.tv/kraken/channels/seeingblue/follows?limit=1&offset=0")
Dim root As JObject = JObject.Parse(json)
Dim stream As JToken = root("follows")
Dim game As String = stream("created_at").ToString()
'Dim viewers As String = stream("_links").ToString()
MsgBox(game)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.OkOnly, "Error")
End Try
End Sub
我收到一条错误消息,指出Accessed JArray values with invalid key value: "created_at". Array position index expected.
有人可以在这里解释我做错了什么吗?
查看格式化的JSON可能会有所帮助。我将代码中的URL粘贴到JSONLint.com的验证器窗口中,然后单击“验证”。结果如下:
{
"follows": [
{
"created_at": "2014-10-02T17:15:10Z",
"_links": {
"self": "https://api.twitch.tv/kraken/users/sleepyynet/follows/channels/seeingblue"
},
"user": {
"_id": 41403351,
"name": "sleepyynet",
"created_at": "2013-03-16T19:42:01Z",
"updated_at": "2014-10-07T19:28:33Z",
"_links": {
"self": "https://api.twitch.tv/kraken/users/sleepyynet"
},
"display_name": "SleepyyNet",
"logo": "http://static-cdn.jtvnw.net/jtv_user_pictures/sleepyynet-profile_image-96061b55b0da4c11-300x300.png",
"bio": "Zzz...The Tired One",
"type": "user"
}
}
],
"_total": 14,
"_links": {
"self": "https://api.twitch.tv/kraken/channels/seeingblue/follows?direction=DESC&limit=1&offset=0",
"next": "https://api.twitch.tv/kraken/channels/seeingblue/follows?direction=DESC&limit=1&offset=1"
}
}
你可以从上面看到,JSON响应对象包含三个属性:follows
,_total
,和_links
。该follows
属性包含一个数组。该数组包含一个对象,它具有三个属性:created_at
,_links
和user
。created_at
是包含日期的字符串,而user
和_links
都是包含更多属性的对象。
考虑到这一点,让我们看看您的代码在做什么。
在第一个示例中,您正在执行以下操作:
Dim stream As JToken = root("user")
Dim game As String = stream("name").ToString()
失败是因为user
JSON的根级别没有属性,所以root("user")
返回null。当您尝试stream
在下一行使用null变量时,您将获得一个异常。
在第二个示例中,您正在执行以下操作:
Dim stream As JToken = root("follows")
Dim game As String = stream("created_at").ToString()
失败是因为root("follows")
返回一个数组。您不能JArray
使用字符串名称为a编制索引;您必须使用数字索引(或者使用For Each
循环对数组进行迭代)。
那么我们如何才能使这项工作呢?让我们举第一个例子。要在响应根目录的follows数组的第一项中获取用户名,可以执行以下操作:
Dim root As JToken = JToken.Parse(json) ' Parse the response
Dim follows As JToken = root("follows") ' Get the "follows" array from the root
Dim item As JToken = follows(0) ' Get the first item of the array
Dim user As JToken = item("user") ' Get the user object from the item
Dim name As String = user("name") ' Get the name from the user object
MsgBox(name) ' Display the name
用更少的代码完成相同操作的另一种方法是使用方便的SelectToken
方法,JToken
使用路径语法直接导航到所需的位置:
Dim root As JToken = JToken.Parse(json)
Dim name As String = root.SelectToken("follows[0].user.name")
MsgBox(name)
当然,以上两个示例均假定您已经具有所需的数组项的索引。如果只有一项,那么没问题-索引是0
。但是,如果数组中有多个项目怎么办?在这种情况下,您可能需要循环处理。这是一个显示“ seeingblue”之后所有用户名称的示例。
Dim url As String = "https://api.twitch.tv/kraken/channels/seeingblue/follows"
Dim json As String = New WebClient().DownloadString(url)
Dim root As JToken = JToken.Parse(json)
Dim sb As New StringBuilder()
For Each item As JToken In root("follows")
sb.AppendLine(item.SelectToken("user.name"))
Next
MsgBox(sb.ToString())
希望这可以帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句