Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在VB.NET中使用JSON.NET组织数据_Vb.net_Json.net - Fatal编程技术网

在VB.NET中使用JSON.NET组织数据

在VB.NET中使用JSON.NET组织数据,vb.net,json.net,Vb.net,Json.net,我想首先说我不知道我在做什么。我正在尝试从Twitch.tv读取json数据。我当前使用的URL如下所示: 在我的程序中,我成功地反序列化了我的数据,使其看起来整洁易读。我在以有组织的方式存储数据时遇到问题。我想做的是定期轮询URL并检查是否有任何更改 我能够让这个例子工作,但我不能修改代码来满足我的需要。 这就是我目前拥有的: Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

我想首先说我不知道我在做什么。我正在尝试从Twitch.tv读取json数据。我当前使用的URL如下所示:

在我的程序中,我成功地反序列化了我的数据,使其看起来整洁易读。我在以有组织的方式存储数据时遇到问题。我想做的是定期轮询URL并检查是否有任何更改

我能够让这个例子工作,但我不能修改代码来满足我的需要。

这就是我目前拥有的:

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
但是我刚刚得到一个错误,关于
对象引用没有设置为对象的实例。

或者如果我尝试

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

我得到一个错误,声明
访问的JArray值的键值无效:“created_at”。需要数组位置索引。
有人能解释一下我在这里做错了什么吗?

查看格式化的JSON可能会有所帮助。我将您代码中的URL粘贴到位于的验证程序窗口中,并单击“验证”。结果如下:

{
    "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
\u total
\u links
后面的
属性包含一个数组。该数组包含一个对象,该对象有三个属性:
处创建、
\u链接
用户
created\u at
是一个包含日期的字符串,而
user
\u links
都是包含更多属性的对象

考虑到这一点,让我们看看您的代码在做什么

在第一个示例中,您正在执行以下操作:

Dim stream As JToken = root("user")
Dim game As String = stream("name").ToString()
Dim stream As JToken = root("follows")
Dim game As String = stream("created_at").ToString()
这会失败,因为JSON的根级别没有
user
属性,因此
root(“user”)
返回null。当您尝试在下一行使用null
变量时,会出现异常

在第二个示例中,您正在这样做:

Dim stream As JToken = root("user")
Dim game As String = stream("name").ToString()
Dim stream As JToken = root("follows")
Dim game As String = stream("created_at").ToString()
这会失败,因为
root(“followers”)
返回一个数组。不能使用字符串名称为JArray
编制索引;必须使用数字索引(或者对每个循环使用
迭代数组)


那么,我们如何才能让这一切顺利进行呢?让我们举你的第一个例子。要在响应根中的以下数组的第一项中获取用户名,可以执行以下操作:

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”后面的所有用户的名称


希望这有帮助。

你不能用字符串索引数组。所以我想两者的区别在于示例不是数组,而我的示例是数组。你知道我该怎么做吗?谢谢,很容易理解。我会仔细考虑几个小时,我相信我能从中得到一些有用的东西。