无法将Google Assistant的Buffer响应解析为String

时间:2019-07-12 08:19:25

标签: node.js string buffer google-assistant-sdk

我正在使用Google Assistand SDK来查询命令,它实际上运行良好,但是在查询响应中,它返回的sessionState作为Buffer,已经尝试了多种方法将Buffer解析为字符串,但是没有运气< / p>

https://hackernoon.com/https-medium-com-amanhimself-converting-a-buffer-to-json-and-utf8-strings-in-nodejs-2150b1e3de57处尝试的示例类似于我在StackOverFlow上找到的示例

在JSON响应中,我收到:

"conversationState":{"type":"Buffer","data":[10,38,67,35,53,100,57,53,53,53,53,51,45,48,48,48,48,45,50,53,97,49,45,57,51,98,54,45,50,52,48,53,56,56,54,100,51,56,53,48,18,231,1,75,106,57,90,101,110,100,74,86,109,49,117,88,49,70,79,83,88,112,66,84,107,86,73,77,109,70,119,89,108,86,51,87,108,74,76,90,85,112,81,100,110,82,97,98,69,49,89,87,68,78,116,76,86,74,54,82,49,70,66,81,85,70,69,81,86,90,108,97,50,78,82,77,108,82,87,101,107,112,51,81,85,70,66,81,85,69,52,114,79,71,103,54,81,86,54,86,119,111,85,67,65,69,83,69,68,65,119,77,68,65,119,77,68,100,104,78,71,82,106,90,71,81,120,79,68,85,83,80,49,108,54,100,48,108,87,98,87,53,102,85,85,53,74,101,107,70,79,82,85,103,121,89,88,66,105,86,88,100,97,85,107,116,108,83,108,66,50,100,70,112,115,84,86,104,89,77,50,48,116,85,110,112,72,85,85,70,66,81,85,82,66,86,109,86,114,89,49,69,121,86,70,90,54,83,110,100,66,81,85,70,66,81,89,111,66,67,103,106,51,55,75,45,66,54,100,121,108,50,66,85]}

我的工作是:当我收到带有许多参数的对象时,我仅提取对话状态并将其解析为JSON

let conversationJson = JSON.stringify(conversationState);

然后我将其发送到缓冲区解析器:

let bufferOriginal = Buffer.from(JSON.parse(conversationJson).data);
let buffer = Buffer.from(bufferOriginal);

最后在控制台中打印它:

console.log(`BufferDataUTF: ${buffer.toString('utf8')}`);

因此,在执行示例所说明的操作后,我应该收到以下格式的缓冲区:

缓冲区xx xx xx xx xx xx xx xx xx

来自de Json,但我却收到:

&C#5d952c74-0000-21f7-a186-2405886e4e48Kj9ZendJVm1uX1FOSXpjRmxNM2YwbS1WeFdGcFZRdmhaRUgyU3JiazBaUkFBQUFEQVZla2NRMlRWek53QUFBQUE4ieeg6QV6VwoUCAESEDAwMDAwMDdhNGRjZGQxODUSP1l6d0lWbW5fUU5JemNGbE0zZjBtLVZ4V0ZwVlF2aFpFSDJTcmJrMFpSQUFBQURBVmVrY1EyVFZ6TndBQUFBQYoBCgjczrqskvKl2BU

任何想法可能有什么问题吗?谢谢

1 个答案:

答案 0 :(得分:0)

这有点故意。 conversation_state值是一个唯一的标识符,它允许助手为设备上的给定对话保留上下文。这样可以使诸如后续查询之类的事情起作用。

在文档中,其类型为bytes。对于您的目的,这是一个不透明,无用的字符串。但是,为了保留上下文,您需要对其进行跟踪并在每个后续请求中发送它。