views:

32

answers:

1

I'm work on a tool for learning purposes, it perform a search with google apis. Using HTTPSocket I get the results of the search in json format and then parse it to dictionary with the json.parser written by CharcoalDesign.co.uk

This is how looks json results:

{"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}

The problem that i want to loop each value of "results" and add data to a listbox, without adding any other responseData (such as "cursor").

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")

After that i dont know how to loop each of "results" value, i've tryed many ways with for-each... works with dictionary, "for each key in d.Keys()", but not with collection. Where i'm wrong?

+1  A: 

To loop through a collection you need to access it through the Items function.

for i as integer = 1 to c.count //Collection is 1 based
   dim s as string
   s = c.item(i)
next
BKeeney Software
@BKeeney: Thanks! Now i've understand the correct synthax to use in a loop with collections, but there's something wrong with my code... i get an error on the line with "s = c.item(i)". Is this the right way the c.collection from d.dictionary?
Luciano
Well, I've never used the CharcoalDesign json parser. Just guessing, but try using something like this: dim s as string = d.value("results")
BKeeney Software
Or dim v as variant = d.value("results") and look in the debugger to see what it returns.
BKeeney Software
Using the loop i get s as nil. Tryed with dim s as string = d.value("results"), s is an empty string. And v as variant = d.value("results"), v is a collection with count=4. Thanks again for your help!
Luciano
@Bkeeney, Nice to see you on StackOverflow ;)
Jordan
I finally menaged where's the problem... d.value("results") return each value as dictionary, so with a simply for each i can loop thru the results! Thanks again, it helped so much!
Luciano