【问题标题】:Loop a collection with Realbasic使用 Realbasic 循环集合
【发布时间】:2010-10-26 17:23:54
【问题描述】:

我正在开发一种用于学习目的的工具,它使用 google api 执行搜索。使用 HTTPSocket 我以 json 格式获取搜索结果,然后使用 CharcoalDesign.co.uk 编写的 json.parser 将其解析为字典

这是 json 结果的样子:

{"responseData": {
 "results": [
  {
   "GsearchResultClass": "GwebSearch",
   "unescapedUrl": "http://en.wikipedia.org/wiki/Paris_Hilton",
   "url": "http://en.wikipedia.org/wiki/Paris_Hilton",
   "visibleUrl": "en.wikipedia.org",
   "cacheUrl": "http://www.google.com/search?q\u003dcache:TwrPfhd22hYJ:en.wikipedia.org",
   "title": "\u003cb\u003eParis Hilton\u003c/b\u003e - Wikipedia, the free encyclopedia",
   "titleNoFormatting": "Paris Hilton - Wikipedia, the free encyclopedia",
   "content": "\[1\] In 2006, she released her debut album..."
  },
  {
   "GsearchResultClass": "GwebSearch",
   "unescapedUrl": "http://www.imdb.com/name/nm0385296/",
   "url": "http://www.imdb.com/name/nm0385296/",
   "visibleUrl": "www.imdb.com",
   "cacheUrl": "http://www.google.com/search?q\u003dcache:1i34KkqnsooJ:www.imdb.com",
   "title": "\u003cb\u003eParis Hilton\u003c/b\u003e",
   "titleNoFormatting": "Paris Hilton",
   "content": "Self: Zoolander. Socialite \u003cb\u003eParis Hilton\u003c/b\u003e..."
  },
  ...
 ],
 "cursor": {
  "pages": [
   { "start": "0", "label": 1 },
   { "start": "4", "label": 2 },
   { "start": "8", "label": 3 },
   { "start": "12","label": 4 }
  ],
  "estimatedResultCount": "59600000",
  "currentPageIndex": 0,
  "moreResultsUrl": "http://www.google.com/search?oe\u003dutf8\u0026ie\u003dutf8..."
 }
}
, "responseDetails": null, "responseStatus": 200}

我想循环“结果”的每个值并将数据添加到列表框,而不添加任何其他响应数据(例如“光标”)的问题。

Dim d as Dictionary
Dim c as Collection 

data = Json.parse(content) // use the class json.parse
d = data.Value("responseData")
c = d.Value("results")

在那之后我不知道如何循环每个“结果”值,我尝试了很多方法 for-each... 适用于字典,“对于 d.Keys() 中的每个键”,但不适用于收藏。我哪里错了?

【问题讨论】:

    标签: collections dictionary realbasic


    【解决方案1】:

    要遍历集合,您需要通过 Items 函数访问它。

    for i as integer = 1 to c.count //Collection is 1 based
       dim s as string
       s = c.item(i)
    next
    

    【讨论】:

    • @BKeeney:谢谢!现在我已经了解了在带有集合的循环中使用的正确合成器,但是我的代码有问题......我在“s = c.item(i)”行出现错误。这是来自 d.dictionary 的 c.collection 的正确方式吗?
    • 好吧,我从未使用过 CharcoalDesign json 解析器。只是猜测,但尝试使用这样的东西: dim s as string = d.value("results")
    • 或将 v 调暗为 variant = d.value("results") 并在调试器中查看它返回的内容。
    • 使用循环我得到 s 为零。尝试使用 dim s as string = d.value("results"), s 是一个空字符串。而 v 作为变体 = d.value("results"),v 是一个 count=4 的集合。再次感谢您的帮助!
    • @Bkeeney,很高兴在 StackOverflow 上见到你;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-12
    • 2016-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多