使用 Python 解析 JSON 响应 - 使用 Automation 360 Python 脚本执行 Python 功能以构建 机器人 。 使用 Python 函数从 REST Web Services GET 请求解析 JSON 响应。

Automation 360

使用 Automation 360 Python 脚本执行 Python 功能以构建 机器人。 使用 Python 函数从 REST Web Services GET 请求解析 JSON 响应。

开始之前

要构建 机器人,请确保满足以下要求:

  • 对 Python 编程语言的基本理解。
  • 创建 Automation Anywhere 机器人 的基本经验。
  • 下载并安装 Python 3。

    下载 Python

    执行以下任一操作:

    • 安装 Python 时,确保在 Python 安装期间选择将 Python x.xx 添加到 PATH
      安装 Python
    • 将路径添加到 Environment 变量。

过程

  1. 登录到 Control Room
  2. 在左侧窗格中,单击自动化
    将显示可用 和表单的列表。
  3. 单击创建机器人图标。
    创建 机器人
  4. 名称字段中,输入 PythonTutorial
  5. 单击创建和编辑
  6. 在操作窗格中,单击变量以创建以下新变量:
    1. 名称 dResponse类型 > 字典子类型 > 字符串
    2. 名称 dRetrieveValue类型 > 字典子类型 > 任意
    3. 名称 dResponseBody类型 > 字符串
    4. 名称 sFullName类型 > 字符串
    5. 名称 sLocation类型 > 字符串
    6. 名称 sTotalUserCount类型 > 字符串
    7. 名称 nTotalUserCount类型 > 数字
    8. 名称 nCurrentUser类型 > 数字
    9. 名称 sCurrentUser类型 > 字符串
    10. 名称 prompt-assignment类型 > 字符串
  7. 从操作窗格中,选择REST Web Services > GET 方法,并将其置于 机器人 流程的开始下方。
    1. URI 字段中,输入 https://randomuser.me/api/?results=5&inc=name,email,location&nat=us
      这是一个示例 API,可将随机用户详细信息返回给调用应用程序。
    2. 验证模式下拉列表中,选择无验证

      python-rest-sample
    3. 将输出分配给变量下拉列表中,选择 dResponse -Dictionary of Strings
  8. 从操作窗格中,单击字符串 > 分配操作,并将其拖动到 REST Web Services 操作下方的画布中。
    1. 选择源字符串变量值字段中,输入 $dResponse{Body}$

      python-string-assign
    2. 选择目标字符串变量下拉列表中,选择 dResponseBody - String
  9. 从操作窗格中,单击 Python 脚本 > 打开 并将其拖到 字符串 > 分配 操作下方的画布中。
    1. Python 字段中,选择手动输入

      python-script-open
    2. 将以下代码复制并粘贴到在此输入脚本字段。
      import json def get_node_count(response): #parse response as json response_dict=json.loads(response) # Create list from JSON body response_body = response_dict['results'] #return the count of entries in JSON body as string lengthasstring = str(len(response_body)) return lengthasstring def get_full_name(dictRequest): itemCount = int(dictRequest['count']) #parse response as json response_dict=json.loads(dictRequest['response']) # Create list from JSON body response_body = response_dict['results'] #Extract values to return return response_body[itemCount]['name']['first'] + " " + response_body[itemCount]['name']['last'] def get_location(dictRequest): itemCount = int(dictRequest['count']) #parse response as json response_dict=json.loads(dictRequest['response']) # Create list from JSON body response_body = response_dict['results'] #Extract values to return return response_body[itemCount]['location']['city'] + ", " + response_body[itemCount]['location']['state']
    3. Python 运行时版本字段中,保留默认值为 3
  10. 从操作窗格中,单击 Python 脚本 > 执行函数,并将其拖入画布的 Python 脚本 > 打开 操作下方。
    1. Python 会话字段中,保留默认值
    2. 输入要执行的函数的名称字段中,输入 get_node_count
    3. 函数参数下拉列表中,选择 dResponseBody - String

      python-execute
    4. 将输出分配给变量下拉列表中,选择 sTotalUserCount - String
  11. 从操作窗格中,单击 字典 > 放置,并将其拖入 Python 脚本 > 打开 操作下方的画布中。
    1. 字典变量字段中,选择 dRetrieveValue -Dictionary
    2. 关联到此键字段中,输入响应
    3. 新值下拉列表中,选择 dResponseBody - String
    4. 为变量分配上一个值下拉列表中,选择 prompt-assignment - String

      python-dict-open
  12. 从操作窗格中,单击字符串 > 转换为数字,并将其拖到 字典 > 放置 操作下方的画布中。
    1. 输入字符串字段中,输入 $sTotalUserCount$
    2. 将输出分配给变量下拉列表中,选择 nTotalUserCount - Number
      python-string-tonum
  13. 从操作窗格中,单击 循环 > 循环
    1. 循环类型 > 迭代器 中,从下拉列表中选择 n 次
    2. 时间字段中,输入 $nTotalUserCount$
    3. 将当前值分配给变量下拉列表中,选择 nCurrentUser - Number
      python-loop
  14. 从操作窗格中,单击数字,选择减量并将其放入循环操作中。
    1. 输入号码字段中,输入 $nCurrentUser$
    2. 输入减量值字段中,输入 1
    3. 将输出分配给变量下拉列表中,选择 nCurrentUser - Number
      python-num-decrement
  15. 从操作窗格中,单击数字,选择转换为字符串并将其放入数字 > 递减下方的循环操作中。
    1. 输入数字字段中,输入 $nCurrentUser$
    2. 输入小数点后的位数字段中,输入 0
    3. 将输出分配给变量下拉列表中,选择 sCurrentUser - String
      python-num-tostring
  16. 从操作窗格中,单击字典 > 放置并将其放入循环操作中。
    1. 字典变量字段中,选择 dRetrieveValue -Dictionary
    2. 关联到此键字段中,输入计数
    3. 新值下拉列表中,选择 sCurrentUser - String
    4. 为变量分配上一个值下拉列表中,选择 prompt-assignment - String
      python-dict-put
  17. 从操作窗格中,单击 Python 脚本 > 执行函数 并将其放入 循环 操作中。
    1. Python 会话字段中,保留默认值
    2. 输入要执行的函数的名称字段中,输入 get_full_name
    3. 函数参数下拉列表中,选择 dRetrieveValue - Dictionary
    4. 将输出分配给变量下拉列表中,选择 sFullName - String
      python-script-execute
  18. 从操作窗格中,单击 Python 脚本 > 执行函数 并将其放入 循环 操作中。
    1. Python 会话字段中,保留默认值
    2. 输入要执行的函数的名称字段中,输入 get_location
    3. 函数参数下拉列表中,选择 dRetrieveValue - Dictionary
    4. 将输出分配给变量下拉列表中,选择 sLocation - String
      python-script-execute-location
  19. 从操作窗格中,单击消息框并将其放入循环操作中。
    1. 输入要显示的消息字段中输入以下值:
      全名:$sFullName$ 位置:$sLocation$
    2. 选择 > 秒数后关闭消息框,输入 5
      python msgbox
  20. 从操作窗格中,单击Python 脚本 > 关闭,并将其放置在循环操作外部。
    1. Python 会话字段中,保留默认值
      python close
  21. 单击保存 以保存您的 机器人 ,然后单击运行
    机器人 运行,显示五个完整的用户名及位置,每个用户名及位置大约显示 5 秒钟,然后成功完成执行。

如果您收到机器人错误消息,请参阅以下部分以获取解决方案:Getting Bot Error while executing Python Script/Function (A-People login required)Getting Bot Error or True value in message box while executing Python Script (A-People login required)An unexpected error occurred while executing Python Script command; Error code: bot.execution.error (A-People login required)