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

问题描述 投票:0回答:1

我做了一个聊天机器人,它将'place'作为输入并连接到一个外部API服务,将'place'作为参数传递。 API服务返回给定位置的所有可用机场详细信息的列表作为JSON对象。

例如,如果在对话中给出'Berlin'作为输入,则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”:“Germany”,“PlaceId”:“BERL-sky”,“PlaceName”:“柏林”, “RegionId”: “”},{ “CityId”: “BERL天空”, “CountryId”: “DE天空”, “国家或地区名称”: “德国”, “PlaceId”: “通心络天空”, “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美国”, “PlaceId”: “BTV-的天空”, “地名”: “伯灵顿”, “RegionId”: “VT”},{ “CityId”: “佛光天空”, “CountryId”: “美天” ,“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“}]

我必须从JSON对象访问第一个'CityId'并将其显示为输出,在柏林作为输入的情况下,预期输出为,

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

我尝试了响应文本,The first airport city id is: $TheResult.message.Places.CityId它会抛出对话框节点错误

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

和响应文本,The first airport city id is: $TheResult.message.Places[0].CityId也没有工作。它没有产生任何错误,但显示与上面相同的输出,只在末尾添加额外的[0] .CityId。

第一个机场城市id是:[{“CityId”:“BERL-sky”,“CountryId”:“DE-sky”,“CountryName”:“Germany”,“PlaceId”:“BERL-sky”,“PlaceName” : “柏林”, “RegionId”: “”},{ “CityId”: “BERL天空”, “CountryId”: “DE-天空”, “国家或地区名称”: “德国”, “PlaceId”:“TXL-天空“,”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“: “美国”,“PlaceId”:“BTV-sky”,“PlaceName”:“Burlington”,“RegionId”:“VT”},{“CityId”:“BLIA-sky”,“CountryId”:“US- sky“,”CountryName“:”美国“,”PlaceId“:”BLI-sky“,”PlaceName“:”Bellingham“,”RegionId“:”WA“},{”CityId“:”BRLA-sky“, “CountryId”:“US-sky”,“CountryName”:“United States”,“PlaceId”:“BRL-sky”,“PlaceName”:“Burlington”,“RegionId”:“IA”}] [0]。 CityId

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

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

json ibm-watson watson-conversation
1个回答
0
投票

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

The first airport code is <? $TheResult.message.Places[0].CityId ?>
© www.soinside.com 2019 - 2024. All rights reserved.