Jump to content

Recommended Posts

Posted

Ok I have been working with a REST API and trying to populate a combo box with a specific part of the return.  I was unable to find a JSON UDF that I could make work so I just started dealing with it as a string and dumping it into an array.

 

I have gotten all of the information trimmed away that I dont want with the exception of the front of the response "7-"Name":"Name of what I want".  I first started working with this in a 1D array so it looks like below:

 

Row  Col0

0       7-"Name":"Name of what I want

1       37-"Name":"Name of what I want

2       7-"Name":"Name of what I want

 

What I need to get rid of is everything to the left of the : leaving me just with the "Name of what I want" section.

 

I have also tried using ArrayToString and separating my rows with | and then doing a stringtrim but I get hung up since my lengths are not the same.  I am more than likely making this too hard on myself.  I can also provide a text file of the response I am working with.  I will be remarking that part out of this code once I can deal directly with the response from the REST API.

;Includes
#Include <WinAPI.au3>
#include <Array.au3>
#include <String.au3>


$restReq = ObjCreate("winhttp.winhttprequest.5.1")
$restGuid = _CreateGuid()

;Dialogs for testing remark out for production use
MsgBox (4096,"My Computer Name",@ComputerName)
MsgBox (4096,"My Name",@UserName)
MsgBox(4096, "Generate Guid", $restGuid)


;REST Request Section
$restReq.Open("GET", "http://kcm-rev-t01/RevitServerAdminRESTService2013/AdminRESTService.svc/ /Contents", False)

$restReq.setRequestHeader ("User-Name", @UserName)
$restReq.setRequestHeader ("User-Machine-Name", @ComputerName)
$restReq.setRequestHeader ("Operation-GUID", $restGuid)

$restReq.Send()

$oReceived = $restReq.ResponseText
$oStatusCode = $restReq.Status

;Dialog box of status code for troubleshooting
MsgBox(4096, "Status Code", $oStatusCode())

If $oStatusCode == 200 then
 $file = FileOpen("Received.txt", 2) ; The value of 2 overwrites the file if it already exists
 FileWrite($file, $oReceived)
 FileClose($file)
 MsgBox(4096, "My test for Nolan", $oReceived ())

;Creates an array broken down by line
Local $array1 = _StringExplode($oReceived, ",", 0)
_ArrayDisplay($array1, "StringExplode 1")


; Pull out and create an array of just the folder names
local $aTmp = __arrayfindall($array1,'Name',1,0)
;MsgBox(4690,"This is my test", $aTmp ())


local $jlc = StringSplit($aTmp, '|',1)
_arraydisplay($jlc)
_ArrayDelete($jlc, 0)
_arraydisplay($jlc)


;Trim out the first 8 characters of col1 in my array
_ArrayTrim($jlc, 1, 1)
_ArrayDisplay($jlc, "$avArray AFTER _ArrayTrim() right 1 character from items 1 to 3")

;turn my array back into a string seperated by |
local $aString = _ArrayToString($jlc, '|')
MsgBox(4690,"Pounding head on desk", $aString ())






EndIf

;-------Section for called functions------------------------------------------------------
Func _CreateGuid()
    Local $Guid = DllStructCreate($tagGUID)

    $Result = DllCall("OLE32.DLL", "dword", "CoCreateGuid", "ptr", DllStructGetPtr($Guid))
    $Result = _WinAPI_StringFromGUID(DllStructGetPtr($Guid))



 $strresult = StringTrimLeft($Result, 1)
 $strresult = StringTrimRight($strresult, 1)

 Return $strresult


EndFunc


func __arrayfindall(byref $array,$srch,$ret_type = 1,$fuzzy = 1)

    ;  usage
    ;    $array    - array to search
    ;    $srch     - string to search for
    ;    $ret_type - 0 = return 2D array of ele # and value
    ;                 1 = return a '|' delimited string of element#-value
    ;     $fuzzy    - 0 = find any ocurrence of string within the element
    ;               - 1 = element must strictly equal the string (default)

    local $tmp, $aTmp[ubound($array)][2],$ele = 0

    for $1 = 0 to ubound($array) - 1

        switch $fuzzy
            case 1
                if $array[$1] == $srch Then
                    $aTmp[$ele][0] = $1
                    $aTmp[$ele][1] = $array[$1]
                    $ele += 1
                endif
            case 0
                if stringregexp($array[$1],$srch) = 1 Then
                    $aTmp[$ele][0] = $1
                    $aTmp[$ele][1] = $array[$1]
                    $ele += 1
                endif
        endswitch

    next

    ; find # of hits

    local $row_cnt
    for $1 = 0 to ubound($aTmp) - 1
        if stringlen($aTmp[$1][0]) > 0  then ContinueLoop
        $row_cnt = $1
        ExitLoop
    next

    ; shrink array

    redim $aTmp[$row_cnt+1][2]

    if $ret_type = 0 then
        return $aTmp
    Else
        for $1 = 0 to ubound($aTmp) - 1
            $tmp &= $aTmp[$1][0] & '-' & $aTmp[$1][1] & '|'
        Next
        return stringtrimright($tmp,3)
    EndIf

endfunc
Posted

Assume $var is your string, then I would just use the StringMid and StringInStr commands as follows;

StringMid($var,StringInStr($var,":")+2)
 

This is assuming the " are part of the string.  If you use the StringMid function without a character count, it will just return the rest of the string starting at your startpos (in this case, 2 characters after the first colon).

Posted

I am also open to input as to process of dealing with the response files from a REST API.  From the documentation I know that the response is in JSON but this is my first dealing dealing with that type of return.

Posted

There is like two JSON UDFs in the example section.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...