-1

I am trying to process this json document using JSON.NET.

With the VB.NET code:

        Dim o As JObject = JObject.Parse(json)
        Dim results As List(Of JToken) = o.Children().ToList

        Dim count As Integer = 0
        For Each item As JProperty In results
            Dim snippet As String = String.Empty
            Dim URL As String = String.Empty
            Dim source As String = String.Empty
            item.CreateReader()
            Select Case item.Name
                Case "response"
                    snippet = item.Last.SelectToken("docs").First.Item("snippet").ToString
                    URL = item.Last.SelectToken("docs").First.Item("web_url").ToString
                    source = ControlChars.NewLine & "<font size='2'>" & item.First.SelectToken("docs").First.Item("source").ToString & "</font>" & ControlChars.NewLine
                    tbNews.Text &= "<a href=" & URL & " target='new'>" & snippet & "</a> - " & source
            End Select
        Next

I am only receiving the first document as a result. Can someone advise as to how I can get the 1st - Nth documents as a complete list?

SteveMcG
  • 1
  • 1
  • 3
  • Did you first create a JToken class with all the appropriate properties? – Ross Brasseaux Feb 11 '14 at 18:20
  • SteveMcG 1) Paste your json to [this site](http://json2csharp.com/), it will create c# classes 2) Copy those classes and paste to [this site](http://www.developerfusion.com/tools/convert/csharp-to-vb) Now you have VB classes 3) Use `JsonConvert.DeserializeObject` to deserialize to `RootObject`. Now you have a compile time safe classes – L.B Feb 11 '14 at 18:56
  • The link to your JSON is broken, you should list the JSON in your question – Rich Feb 07 '20 at 20:47

1 Answers1

1

The docs are 2 levels deep, you're only looping in the top level. Try this...

Dim parsedObject = JObject.Parse(json)
Dim docs = parsedObject("response")("docs")

For Each doc In docs
    Dim snippet As String = doc("snippet")
    Dim URL As String = doc("web_url")
    Dim source As String = doc("source")

    '....

Next
Anthony Chu
  • 37,170
  • 10
  • 81
  • 71