questions/43357824/vb-net-loop-through-json-and-get-all-values

VbNet · Console

/Program.vb

Imports System
Imports System.Linq
Imports System.Collections.Generic
Imports System.Xml.Linq
Imports System.Xml

Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Imports Newtonsoft.Json.Converters
Imports Newtonsoft.Json.Serialization

' https://stackoverflow.com/questions/43357824/vb-net-loop-through-json-and-get-all-values
				
Public Module Module1
	Public Sub Main()
		Console.WriteLine("Json.NET version: " + GetType(JsonSerializer).Assembly.FullName)
        Console.WriteLine("")
		
		Dim jsonString As String = GetJson()
		
		Dim jo = Newtonsoft.Json.Linq.JObject.Parse(jsonString)
		
		Dim VidID = jo("items")(0)("snippet")("resourceId")("videoId")
		Console.WriteLine("VidID: " + CType(VidID, String))

		TestOriginalLoop(jo)
		
		TestSelectTokens(jo)
		
		TestLinq(jo)
		
	End Sub

	Sub TestOriginalLoop(jo as JObject)

		Console.WriteLine(Environment.NewLine & "Testing original loop: ")
		
		For Each Row In jo
			Console.WriteLine(Row.GetType())
		Next		


	End Sub

	Sub TestSelectTokens(jo as JObject)

		' 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()

		Console.WriteLine(Environment.NewLine & "videoIds from SelectTokens(): ")
				
		Console.WriteLine(JsonConvert.SerializeObject(VidIDs))
				
	End Sub

	Public Sub TestLinq(jo as JObject)

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

		Dim VidIDs = query.ToList()
					
		Console.WriteLine(Environment.NewLine & "videoIds from LINQ: ")
				
		Console.WriteLine(JsonConvert.SerializeObject(VidIDs))

	End Sub			
			
	Public Function GetJson() As String
		
		Dim Jstring As String = <![CDATA[
{
"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"
    }
   }
  }
 ]
}
]]>.Value
			
			Return JString
	End Function
End Module
;