IBM Watson 对话:如何从上下文变量访问 JSON 对象?
IBM Watson conversation: How to access JSON object from context variable?
我制作了一个聊天机器人,它将 'place' 作为输入并连接到外部 API 服务,将 'place' 作为参数传递。 API 服务 returns 给定地点的所有可用机场详细信息的列表作为 JSON 对象。
例如,如果 'Berlin' 作为对话中的输入,API returns 跟随 JSON 对象,然后存储在上下文变量 $TheResult 中,
当输入为 'Berlin'、
时,上下文变量 $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
时,它会生成以下输出
Response from external server:
[{"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"}]
我必须从 JSON 对象访问第一个 'CityId' 并将其显示为输出,在柏林作为输入的情况下,预期输出是,
The first airport city id is: BERL-sky
我尝试了回复文本,The first airport city id is: $TheResult.message.Places.CityId
它抛出以下对话节点错误
Error when updating output with output of dialog
node id [node_1_1551877430730]. Node output is
[{"generic":[{"values":[{"text":"Response from external server:
$TheResult.message.Places.CityId"}],"response_type":"text","selection_policy":"sequential"}]}]
SpEL evaluation error: Expression [ $TheResult.message.Places.CityId ]
converted to [ context['TheResult'].message.Places.CityId ] at
position 37: EL1008E: Property or field 'CityId' cannot be found on
object of type 'JsonArray' - maybe not public?
和回复文本,The first airport city id is: $TheResult.message.Places[0].CityId
也没有用。它没有产生任何错误,但显示与上面相同的输出,只是在末尾附加了额外的 [0].CityId。
The first airport city id is:
[{"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"}][0].CityId
我应该如何解析此 JSON 对象以访问各个键值对?
提前致谢,如有必要,我可以进一步详细说明问题!!
尝试将您的 JSON 路径表达式放入 expression syntax。在那里你可以使用完整的语法。像这样:
The first airport code is <? $TheResult.message.Places[0].CityId ?>
我制作了一个聊天机器人,它将 'place' 作为输入并连接到外部 API 服务,将 'place' 作为参数传递。 API 服务 returns 给定地点的所有可用机场详细信息的列表作为 JSON 对象。
例如,如果 'Berlin' 作为对话中的输入,API returns 跟随 JSON 对象,然后存储在上下文变量 $TheResult 中, 当输入为 'Berlin'、
时,上下文变量 $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
时,它会生成以下输出
Response from external server: [{"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"}]
我必须从 JSON 对象访问第一个 'CityId' 并将其显示为输出,在柏林作为输入的情况下,预期输出是,
The first airport city id is: BERL-sky
我尝试了回复文本,The first airport city id is: $TheResult.message.Places.CityId
它抛出以下对话节点错误
Error when updating output with output of dialog node id [node_1_1551877430730]. Node output is [{"generic":[{"values":[{"text":"Response from external server: $TheResult.message.Places.CityId"}],"response_type":"text","selection_policy":"sequential"}]}] SpEL evaluation error: Expression [ $TheResult.message.Places.CityId ] converted to [ context['TheResult'].message.Places.CityId ] at position 37: EL1008E: Property or field 'CityId' cannot be found on object of type 'JsonArray' - maybe not public?
和回复文本,The first airport city id is: $TheResult.message.Places[0].CityId
也没有用。它没有产生任何错误,但显示与上面相同的输出,只是在末尾附加了额外的 [0].CityId。
The first airport city id is: [{"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"}][0].CityId
我应该如何解析此 JSON 对象以访问各个键值对?
提前致谢,如有必要,我可以进一步详细说明问题!!
尝试将您的 JSON 路径表达式放入 expression syntax。在那里你可以使用完整的语法。像这样:
The first airport code is <? $TheResult.message.Places[0].CityId ?>