我想先说我不知道自己在做什么。我正在尝试从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()
由于JSON的根级别没有user
属性,因此root("user")
返回null,因此此操作失败。当您尝试在下面的行中使用空stream
变量时,会得到一个异常。
在您的第二个示例中,您正在执行以下操作:
Dim stream As JToken = root("follows")
Dim game As String = stream("created_at").ToString()
由于root("follows")
返回一个数组,因此此操作失败。不能使用字符串名称为JArray
编制索引;必须使用数字索引(或者使用For Each
循环迭代数组)。
那么,我们如何才能做到这一点呢?让我们举第一个例子。要在响应根中的以下数组的第一项中获取用户的名称,可以执行以下操作:
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())
希望这能有所帮助。