vb.net - 循环访问JSON并获取所有值

时间:2017-04-11 23:29:08

标签: json vb.net youtube-api

我正在使用以下代码从YouTube API JSON Feed获取数据。但是,我能够获得我想要的数据。它为列表中的行数重复相同的数据。

    Dim n As New WebClient()
    Dim jsonString As String = n.DownloadString("https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2C+id&playlistId=PLCJLiJ8uSJrCpxwz4lmnz1NvvF2SzXbhk&key=YOUR_API_KEY")

    Dim jo = Newtonsoft.Json.Linq.JObject.Parse(jsonString)
    For Each Row In jo
        Dim VidID = jo("items")(0)("snippet")("resourceId")("videoId")
        Response.Write(VidID)
    Next

JSON的一个样本(很抱歉,之前没有添加。想到我做了,完全错过了这个球。)

{
"kind":    "youtube#playlistItemListResponse",
"etag":    "\"m2yskBQFythfE4irbTIeOgYYfBU/YJYLWeQ7ZaYKr3ce1Z9EIjD9WVU\"",
"nextPageToken":    "CAUQAA",
"pageInfo":    {
 "totalResults":    10,
 "resultsPerPage":    5
},
"items":    [
 {
     "kind":    "youtube#playlistItem",
     "etag":    "\"m2yskBQFythfE4irbTIeOgYYfBU/0TyxpFnbR5GigaSoRo2gAPfUHwE\"",
     "id":    "UExDSkxpSjh1U0pyQ3B4d3o0bG1uejFOdnZGMlN6WGJoay41NkI0NEY2RDEwNTU3Q0M2",
     "snippet":    {
      "publishedAt":    "2014-12-10T21:16:39.000Z",
      "channelId":    "UCVUx0VcNxnHx7ZjuZK5Sthw",
      "title":    "Private    video",
      "description":    "This    video    is    private.",
      "channelTitle":    "Late    Show    with    David    Letterman",
      "playlistId":    "PLCJLiJ8uSJrCpxwz4lmnz1NvvF2SzXbhk",
      "position":    0,
      "resourceId":    {
          "kind":    "youtube#video",
          "videoId":    "4bVgilYncao"
      }
     }
 },
 {
     "kind":    "youtube#playlistItem",
     "etag":    "\"m2yskBQFythfE4irbTIeOgYYfBU/PVF3VoVEG0X1jmV6GeUNhpCIzF4\"",
     "id":    "UExDSkxpSjh1U0pyQ3B4d3o0bG1uejFOdnZGMlN6WGJoay4yODlGNEE0NkRGMEEzMEQy",
     "snippet":    {
      "publishedAt":    "2014-12-10T21:16:58.000Z",
      "channelId":    "UCVUx0VcNxnHx7ZjuZK5Sthw",
      "title":    "Private    video",
      "description":    "This    video    is    private.",
      "channelTitle":    "Late    Show    with    David    Letterman",
      "playlistId":    "PLCJLiJ8uSJrCpxwz4lmnz1NvvF2SzXbhk",
      "position":    1,
      "resourceId":    {
          "kind":    "youtube#video",
          "videoId":    "H9eYkpgeeI8"
      }
     }
 },
 {
     "kind":    "youtube#playlistItem",
     "etag":    "\"m2yskBQFythfE4irbTIeOgYYfBU/BLUJWU0eHjBeEizXySdnzUhi2lQ\"",
     "id":    "UExDSkxpSjh1U0pyQ3B4d3o0bG1uejFOdnZGMlN6WGJoay4wMTcyMDhGQUE4NTIzM0Y5",
     "snippet":    {
      "publishedAt":    "2014-12-10T21:17:14.000Z",
      "channelId":    "UCVUx0VcNxnHx7ZjuZK5Sthw",
      "title":    "Private    video",
      "description":    "This    video    is    private.",
      "channelTitle":    "Late    Show    with    David    Letterman",
      "playlistId":    "PLCJLiJ8uSJrCpxwz4lmnz1NvvF2SzXbhk",
      "position":    2,
      "resourceId":    {
          "kind":    "youtube#video",
          "videoId":    "ONgN2Hgz3XE"
      }
     }
 },
 {
     "kind":    "youtube#playlistItem",
     "etag":    "\"m2yskBQFythfE4irbTIeOgYYfBU/qlX2iXH1WPg7D3-eq7jInjQyj2c\"",
     "id":    "UExDSkxpSjh1U0pyQ3B4d3o0bG1uejFOdnZGMlN6WGJoay41MjE1MkI0OTQ2QzJGNzNG",
     "snippet":    {
      "publishedAt":    "2014-12-10T21:17:30.000Z",
      "channelId":    "UCVUx0VcNxnHx7ZjuZK5Sthw",
      "title":    "Private    video",
      "description":    "This    video    is    private.",
      "channelTitle":    "Late    Show    with    David    Letterman",
      "playlistId":    "PLCJLiJ8uSJrCpxwz4lmnz1NvvF2SzXbhk",
      "position":    3,
      "resourceId":    {
          "kind":    "youtube#video",
          "videoId":    "1Ee4bfu_t3c"
      }
     }
 },
 {
     "kind":    "youtube#playlistItem",
     "etag":    "\"m2yskBQFythfE4irbTIeOgYYfBU/698H3tj04Cx7lZ8z4rHjL4DPtUI\"",
     "id":    "UExDSkxpSjh1U0pyQ3B4d3o0bG1uejFOdnZGMlN6WGJoay4wOTA3OTZBNzVEMTUzOTMy",
     "snippet":    {
      "publishedAt":    "2014-12-10T21:17:47.000Z",
      "channelId":    "UCVUx0VcNxnHx7ZjuZK5Sthw",
      "title":    "Private    video",
      "description":    "This    video    is    private.",
      "channelTitle":    "Late    Show    with    David    Letterman",
      "playlistId":    "PLCJLiJ8uSJrCpxwz4lmnz1NvvF2SzXbhk",
      "position":    4,
      "resourceId":    {
          "kind":    "youtube#video",
          "videoId":    "Bzr5VtFvSyw"
    }
   }
  }
 ]
}

有关在此循环中返回正确数据的任何信息,而不是重复数据? 我需要的数据是每个列表中的videoId。

谢谢大家。

CodingEE

1 个答案:

答案 0 :(得分:0)

Newtonsoft文档页面Querying JSON with LINQQuerying JSON with SelectToken描述了如何在加载后查询嵌套在JObject层次结构中的值。

例如,使用SelectTokens(),您可以按如下方式查询"videoId"值:

' Get all videoId values, convert to strings, and store in an array:
Dim VidIDs = jo.SelectTokens("items[*].snippet.resourceId.videoId") _
        .Select(Function(c) c.ToString()) _
        .ToList()

这里我使用通配符运算符[*]来匹配"items"数组中的所有元素。这是JSONPath query syntax的标准运算符SelectTokens()支持。

这是使用LINQ的等效逻辑:

Dim query = From item In jo("items")
    Let videoId = item("snippet")("resourceId")("videoId").ToString()
    Select videoId
Dim VidIDs = query.ToList()

(请注意,SelectTokens代码会自动跳过缺少视频ID的项目,而LINQ代码将抛出空引用异常。)

您的代码不起作用的原因是,虽然JObject实现了许多不同的IEnumerable<T> intrerfaces,但它直接实现的是public IEnumerator<KeyValuePair<string, JToken>> GetEnumerator()。因此,Row的类型为KeyValuePair<string, JToken>,因此Row("items")无法编译。

示例fiddle