无法将 Dialogflow 响应解析为 AppResponse
Failed to parse Dialogflow response into AppResponse
我在 conv.ask
之前执行数组映射时遇到问题。我收到与格式错误的 JSON 对象相关的错误,但据我所知 应该 有效。
const timetableCells = timetable.Trains.map((item) => {
return {
cells: [item.Line, item.Destination, item.Car, item.Min],
}
})
conv.ask(new Table({
title: `Rail Timetable for ${station}`,
subtitle: 'Timetable as of x date',
image: new Image({
url: 'http://google.com/image.png',
alt: 'Logo'
}),
columns: [
{
header: 'Line',
align: 'LEADING'
},
{
header: 'Destination',
align: 'LEADING'
},
{
header: 'Car',
align: 'LEADING'
},
{
header: 'Arrival',
align: 'LEADING'
},
],
rows: timetableCells,
buttons: new Button({
title: 'Button Title',
url: 'https://github.com/actions-on-google'
})
}))
这是 timetable
的样子:
{"Trains":[{"Car":"8","Destination":"Glenmont","DestinationCode":"B11","DestinationName":"Glenmont","Group":"1","Line":"RD","LocationCode":"B09","LocationName":"Forest Glen","Min":"9"},{"Car":"8","Destination":"Glenmont","DestinationCode":"B11","DestinationName":"Glenmont","Group":"1","Line":"RD","LocationCode":"B09","LocationName":"Forest Glen","Min":"18"},{"Car":"8","Destination":"Glenmont","DestinationCode":"B11","DestinationName":"Glenmont","Group":"1","Line":"RD","LocationCode":"B09","LocationName":"Forest Glen","Min":"36"},{"Car":"8","Destination":"Shady Gr","DestinationCode":"A15","DestinationName":"Shady Grove","Group":"2","Line":"RD","LocationCode":"B09","LocationName":"Forest Glen","Min":""}]}
如果我记录 timetableCells
我有一个看起来像这样的数组,我将它分配给 conv.ask
内部的 rows
这应该等于 Google 助手预计 their documentation.
[ { cells: ['1', '2', '3', '4'] }, { cells: ['1', '2', '3', '4'] } ]
如果我将传递给 conv.ask
的 JSON 对象字符串化,它看起来是正确的:
JSON.parse("{\"title\":\"Rail Timetable for Farragut North\",\"subtitle\":\"Timetable as of x date\",\"columns\":[{\"header\":\"Line\",\"align\":\"LEADING\"},{\"header\":\"Destination\",\"align\":\"LEADING\"},{\"header\":\"Car\",\"align\":\"LEADING\"},{\"header\":\"Arrival\",\"align\":\"LEADING\"}],\"rows\":[{\"cells\":[\"RD\",\"Glenmont\",\"8\",\"\"]},{\"cells\":[\"RD\",\"Shady Gr\",\"8\",\"\"]}]}")
我哪里错了?我知道这一行是导致问题的原因,因为我可以替换它并且它可以正常工作。调试器指向一个 JSON 验证错误,但我似乎看不到它。
我刚刚解决了非常相似的问题。您分配给行和单元格的数组必须仅包含 Strings。在那些是数字的情况下,'failed to parse DialogFlow response int AppResponse' 将会发生。
我花了很长时间才弄明白,奇怪的是这里没有抛出不同的错误。很明显,行中的数据需要与 table 中 headers 的数量相匹配,但不明显的是字符串 不能 为空,否则会出现解析错误。
在某些情况下,某些值从我的 API 响应中返回为空字符串,这导致了问题。将代码更改为以下内容可解决问题,因为它确保它们永远不会为空字符串。
const timetableCells = timetable.Trains.map((item) => {
return {
cells: [item.Line || "N/A", item.Destination || "N/A", item.Car || "TBD", item.Min || "TDB"],
}
})
我在 conv.ask
之前执行数组映射时遇到问题。我收到与格式错误的 JSON 对象相关的错误,但据我所知 应该 有效。
const timetableCells = timetable.Trains.map((item) => {
return {
cells: [item.Line, item.Destination, item.Car, item.Min],
}
})
conv.ask(new Table({
title: `Rail Timetable for ${station}`,
subtitle: 'Timetable as of x date',
image: new Image({
url: 'http://google.com/image.png',
alt: 'Logo'
}),
columns: [
{
header: 'Line',
align: 'LEADING'
},
{
header: 'Destination',
align: 'LEADING'
},
{
header: 'Car',
align: 'LEADING'
},
{
header: 'Arrival',
align: 'LEADING'
},
],
rows: timetableCells,
buttons: new Button({
title: 'Button Title',
url: 'https://github.com/actions-on-google'
})
}))
这是 timetable
的样子:
{"Trains":[{"Car":"8","Destination":"Glenmont","DestinationCode":"B11","DestinationName":"Glenmont","Group":"1","Line":"RD","LocationCode":"B09","LocationName":"Forest Glen","Min":"9"},{"Car":"8","Destination":"Glenmont","DestinationCode":"B11","DestinationName":"Glenmont","Group":"1","Line":"RD","LocationCode":"B09","LocationName":"Forest Glen","Min":"18"},{"Car":"8","Destination":"Glenmont","DestinationCode":"B11","DestinationName":"Glenmont","Group":"1","Line":"RD","LocationCode":"B09","LocationName":"Forest Glen","Min":"36"},{"Car":"8","Destination":"Shady Gr","DestinationCode":"A15","DestinationName":"Shady Grove","Group":"2","Line":"RD","LocationCode":"B09","LocationName":"Forest Glen","Min":""}]}
如果我记录 timetableCells
我有一个看起来像这样的数组,我将它分配给 conv.ask
内部的 rows
这应该等于 Google 助手预计 their documentation.
[ { cells: ['1', '2', '3', '4'] }, { cells: ['1', '2', '3', '4'] } ]
如果我将传递给 conv.ask
的 JSON 对象字符串化,它看起来是正确的:
JSON.parse("{\"title\":\"Rail Timetable for Farragut North\",\"subtitle\":\"Timetable as of x date\",\"columns\":[{\"header\":\"Line\",\"align\":\"LEADING\"},{\"header\":\"Destination\",\"align\":\"LEADING\"},{\"header\":\"Car\",\"align\":\"LEADING\"},{\"header\":\"Arrival\",\"align\":\"LEADING\"}],\"rows\":[{\"cells\":[\"RD\",\"Glenmont\",\"8\",\"\"]},{\"cells\":[\"RD\",\"Shady Gr\",\"8\",\"\"]}]}")
我哪里错了?我知道这一行是导致问题的原因,因为我可以替换它并且它可以正常工作。调试器指向一个 JSON 验证错误,但我似乎看不到它。
我刚刚解决了非常相似的问题。您分配给行和单元格的数组必须仅包含 Strings。在那些是数字的情况下,'failed to parse DialogFlow response int AppResponse' 将会发生。
我花了很长时间才弄明白,奇怪的是这里没有抛出不同的错误。很明显,行中的数据需要与 table 中 headers 的数量相匹配,但不明显的是字符串 不能 为空,否则会出现解析错误。
在某些情况下,某些值从我的 API 响应中返回为空字符串,这导致了问题。将代码更改为以下内容可解决问题,因为它确保它们永远不会为空字符串。
const timetableCells = timetable.Trains.map((item) => {
return {
cells: [item.Line || "N/A", item.Destination || "N/A", item.Car || "TBD", item.Min || "TDB"],
}
})