IBM Watson对话:如何从上下文变量访问JSON对象?

时间:2019-03-08 13:07:47

标签: json ibm-watson watson-conversation

我制作了一个聊天机器人,它以“地方”作为输入并连接到通过“地方”作为参数的外部API服务。 API服务以JSON对象的形式返回给定地点的所有可用机场详细信息的列表。

例如,如果在对话中输入“柏林”,则该API返回以下JSON对象,然后将其存储在上下文变量$ TheResult中, 当输入为“柏林”时,上下文变量$ TheResult将以下JSON对象保留为值,

{
  "message": {
    "Places": [
      {
        "CityId": "BERL-sky",
        "CountryId": "DE-sky",
        "CountryName": "Germany",
        "PlaceId": "BERL-sky",
        "PlaceName": "Berlin",
        "RegionId": ""
      },
      {
        "CityId": "BERL-sky",
        "CountryId": "DE-sky",
        "CountryName": "Germany",
        "PlaceId": "TXL-sky",
        "PlaceName": "Berlin Tegel",
        "RegionId": ""
      },
      {
        "CityId": "BERL-sky",
        "CountryId": "DE-sky",
        "CountryName": "Germany",
        "PlaceId": "SXF-sky",
        "PlaceName": "Berlin Schoenefeld",
        "RegionId": ""
      },
      {
        "CityId": "BTVA-sky",
        "CountryId": "US-sky",
        "CountryName": "United States",
        "PlaceId": "BTV-sky",
        "PlaceName": "Burlington",
        "RegionId": "VT"
      },
      {
        "CityId": "BLIA-sky",
        "CountryId": "US-sky",
        "CountryName": "United States",
        "PlaceId": "BLI-sky",
        "PlaceName": "Bellingham",
        "RegionId": "WA"
      },
      {
        "CityId": "BRLA-sky",
        "CountryId": "US-sky",
        "CountryName": "United States",
        "PlaceId": "BRL-sky",
        "PlaceName": "Burlington",
        "RegionId": "IA"
      }
    ]
  },
  "parameters": {
    "context": "",
    "message": "",
    "service": "https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/autosuggest/v1.0/FI/EUR/en-US/?query=Berlin"
  }
}

当我将响应文本用作Response from external server: $TheResult.message.Places时,它将生成以下输出

  

来自外部服务器的响应:   [{{“ CityId”:“ BERL-sky”,“ CountryId”:“ DE-sky”,“ CountryName”:“德国”,“ PlaceId”:“ BERL-sky”,“ PlaceName”:“柏林”,“ RegionId “:”“},{” CityId“:” BERL-sky“,” CountryId“:” DE-sky“,” CountryName“:”德国“,” PlaceId“:” TXL-sky“,” PlaceName“:”柏林   Tegel“,” RegionId“:”“},{” CityId“:” BERL-sky“,” CountryId“:” DE-sky“,” CountryName“:”德国“,” PlaceId“:” SXF-sky“, “ PlaceName”:“柏林   Schoenefeld“,” RegionId“:”“},{” CityId“:” BTVA-sky“,” CountryId“:” US-sky“,” CountryName“:” United   国家”,“ PlaceId”:“ BTV天空”,“ PlaceName”:“ Burlington”,“ RegionId”:“ VT”},{“ CityId”:“ BLIA天空”,“ CountryId”:“美国天空” ,“ CountryName”:“美国   国家”,“ PlaceId”:“ BLI-sky”,“ PlaceName”:“ Bellingham”,“ RegionId”:“ WA”},{“ CityId”:“ BRLA-sky”,“ CountryId”:“ US-sky” ,“ CountryName”:“美国   国家”,“ PlaceId”:“ BRL天空”,“ PlaceName”:“ Burlington”,“ RegionId”:“ IA”}]

我必须访问JSON对象中的第一个'CityId'并将其显示为输出,如果是柏林作为输入,则预期输出为

  

第一个机场城市ID是:BERL-sky

我尝试了回复文本The first airport city id is: $TheResult.message.Places.CityId 它引发以下对话框节点错误

  

使用对话框的输出更新输出时出错   节点ID [node_1_1551551430730]。节点输出为   [{“ generic”:[{“ values”:[{“ text”:“来自外部服务器的响应:   $ TheResult.message.Places.CityId“}],” response_type“:” text“,” selection_policy“:” sequential“}]}]]   SpEL评估错误:表达式[$ TheResult.message.Places.CityId]   在以下位置转换为[context ['TheResult']。message.Places.CityId]   位置37:EL1008E:无法在以下位置找到属性或字段“ CityId”   类型为“ JsonArray”的对象-可能不是公共的?

和回复文本The first airport city id is: $TheResult.message.Places[0].CityId也无效。它没有产生任何错误,但显示的输出与上述相同,仅在末尾附加了额外的[0] .CityId。

  

第一个机场城市ID是:   [{{“ CityId”:“ BERL-sky”,“ CountryId”:“ DE-sky”,“ CountryName”:“德国”,“ PlaceId”:“ BERL-sky”,“ PlaceName”:“柏林”,“ RegionId “:”“},{” CityId“:” BERL-sky“,” CountryId“:” DE-sky“,” CountryName“:”德国“,” PlaceId“:” TXL-sky“,” PlaceName“:”柏林   Tegel“,” RegionId“:”“},{” CityId“:” BERL-sky“,” CountryId“:” DE-sky“,” CountryName“:”德国“,” PlaceId“:” SXF-sky“, “ PlaceName”:“柏林   Schoenefeld“,” RegionId“:”“},{” CityId“:” BTVA-sky“,” CountryId“:” US-sky“,” CountryName“:” United   国家”,“ PlaceId”:“ BTV天空”,“ PlaceName”:“ Burlington”,“ RegionId”:“ VT”},{“ CityId”:“ BLIA天空”,“ CountryId”:“美国天空” ,“ CountryName”:“美国   国家”,“ PlaceId”:“ BLI-sky”,“ PlaceName”:“ Bellingham”,“ RegionId”:“ WA”},{“ CityId”:“ BRLA-sky”,“ CountryId”:“ US-sky” ,“ CountryName”:“美国   国家”,“ PlaceId”:“ BRL天空”,“ PlaceName”:“ Burlington”,“ RegionId”:“ IA”}] [0] .CityId

我应该如何解析此JSON对象以访问各个键值对?

预先感谢,如有必要,我可以进一步阐述问题!

1 个答案:

答案 0 :(得分:0)

尝试将您的JSON路径表达式放入expression syntax中。在那里您可以使用完整的语法。像这样:

The first airport code is <? $TheResult.message.Places[0].CityId ?>