Created
June 5, 2015 16:52
-
-
Save gsherman/68892a977d2aeb9a3aa4 to your computer and use it in GitHub Desktop.
Consuming JSON in ClearBasic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Option Explicit | |
Type SearchResult | |
Url as String | |
Id as String | |
Title as String | |
Content as String | |
End Type | |
Dim oneResult as SearchResult | |
Sub AddFunctionToScriptObject(scriptControl, functionDefinition) | |
scriptControl.AddCode functionDefinition | |
End Sub | |
Sub AddFunctionsToScriptObject(scriptControl) | |
Dim getNumberOfResultsFunction As String | |
Dim getResultByIndexFunction As String | |
getNumberOfResultsFunction = "function getNumberOfResults(searchResultObject){ return searchResultObject.responseData.results.length;}" | |
AddFunctionToScriptObject scriptControl, getNumberOfResultsFunction | |
getResultByIndexFunction = "function getPropertValueForResultByIndex(searchResultObject, index, prop){ return searchResultObject.responseData.results[index][prop];}" | |
AddFunctionToScriptObject scriptControl, getResultByIndexFunction | |
End Sub | |
Sub getSearchResultByIndex(scriptControl as Object, resultObject as Object, index as Integer) | |
oneResult.Title = scriptControl.Run("getPropertValueForResultByIndex", resultObject, index, "titleNoFormatting") | |
oneResult.Content = scriptControl.Run("getPropertValueForResultByIndex", resultObject, index, "content") | |
oneResult.Id = index | |
oneResult.URL = scriptControl.Run("getPropertValueForResultByIndex", resultObject, index, "url") | |
End Sub | |
Function GetHTTPResponse (url As String, method As String, data As String) as String | |
Dim oHttp as Object | |
Set oHttp = CreateObject("microsoft.XmlHttp") | |
oHttp.open method, url, False | |
oHttp.send ByVal data | |
GetHTTPResponse = oHttp.ResponseText | |
End Function | |
Sub main() | |
Dim url as String | |
Dim terms as String | |
Dim json as String | |
Dim scriptControl As Object | |
Dim searchResultObject As Object | |
Dim numberOfResults as Long | |
Dim index as Integer | |
terms = "dovetail software" | |
terms = "fcsdk powershell" | |
url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=" & terms | |
json = GetHTTPResponse (url,"GET", "") | |
Set scriptControl = CreateObject("MSScriptControl.ScriptControl") | |
scriptControl.Language = "JScript" | |
Set searchResultObject = scriptControl.Eval("(" + json + ")") | |
Debug.Print "Search Response Status: " & searchResultObject.responseStatus | |
AddFunctionsToScriptObject scriptControl | |
numberOfResults = scriptControl.Run("getNumberOfResults", searchResultObject) | |
Debug.Print "Number of Search Results: " & CStr(numberOfResults) | |
For index = 0 to numberOfResults - 1 | |
getSearchResultByIndex scriptControl, searchResultObject, index | |
Debug.Print "" | |
Debug.Print "Search Result # " & cstr(index ) | |
Debug.Print "Title: " & oneResult.Title | |
Debug.Print "URL: " & oneResult.Url | |
Debug.Print "" | |
Next index | |
End Sub | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment