建立新的JSON我收到一個JSON對象,與一些數量從wit.ai快速回復元素,像這樣:如何動態地從舊的JavaScript
"msg": "So glad to have you back. What do you want me to do?
"action_id": "6fd7f2bd-db67-46d2-8742-ec160d9261c1",
"confidence": 0.08098269709064443,
"quickreplies": [
"News?",
"Subscribe?",
"Contribute?",
"Organize?"
],
"type": "msg"
然後我需要將它們轉換爲稍微不同的格式,因爲它們按照以下代碼中所述傳遞給FaceBook Messenger。 Wit只公開'msg'和'quickreplies'。你能提出一個很好的方法來做到這一點?據我所知,它追蹤「console.log(element)」。
if (quickreplies){
// got simple array of quickreplies
// need to format quickreplies for FB:
// "quick_replies":[
// {
// "content_type":"text",
// "title":"Red",
// "payload":"DEVELOPER_DEFINED_PAYLOAD_FOR_PICKING_RED"
// },
// {
// "content_type":"text",
// "title":"Green",
// "payload":"DEVELOPER_DEFINED_PAYLOAD_FOR_PICKING_GREEN"
// }]
console.log('we got quickreplies, here they are:');
var quick_replies = []; // ??
quickreplies.forEach(function(element) {
console.log(element)
});
}
else (console.log('no quickreplies'));
在上面的例子中,最終的結果應該是這樣的:
// "quick_replies":[
// {
// "content_type":"text",
// "title":"News",
// "payload":"News"
// },
// {
// "content_type":"text",
// "title":"Subscribe?",
// "payload":"Subscribe?"
// }
// "content_type":"text",
// "title":"Contribute?",
// "payload":"Contribute?"
// },
// {
// "content_type":"text",
// "title":"Organize?",
// "payload":"Organize?"
// }
// ]
'quickreplies'中的元素是怎樣的? – JohanP
已更新的問題顯示機智反應json – Jerome