【问题标题】:JSON.net - Using VB.NET Unable to iterate through resultsJSON.net - 使用 VB.NET 无法遍历结果
【发布时间】:2014-02-11 18:04:01
【问题描述】:

我正在尝试使用 JSON.NET 处理 this json document

使用 VB.NET 代码:

        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

结果我只收到了第一份文件。有人可以建议我如何将第 1 - N 个文档作为完整列表吗?

【问题讨论】:

  • 您是否首先创建了具有所有适当属性的 JToken 类?
  • SteveMcG 1) 将你的 json 粘贴到 this site,它将创建 c# 类 2) 复制这些类并粘贴到 this site 现在你有 VB 类 3) 使用 JsonConvert.DeserializeObject 反序列化到 @ 987654326@。现在你有了一个编译时安全的类
  • 谢谢!很有帮助!
  • 您的 JSON 链接已损坏,您应该在问题中列出 JSON

标签: asp.net json vb.net json.net


【解决方案1】:

文档有 2 层深,您只在顶层循环。试试这个...

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-08
    • 1970-01-01
    • 1970-01-01
    • 2019-11-30
    • 2010-11-16
    • 2021-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多