如何在JavaScript中动态构建json数组
我从wit.ai收到了一个带有一些快速回复元素的json对象,像这样:
I receive a json object with some number of quick reply elements from wit.ai, like this:
"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时,我需要将它们转换为稍有不同的格式,如下面的代码所述.机智只会显示"msg"和快速回复".您能建议一个好的方法吗?据我了解,它位于"console.log(element)"之后.
I then need to convert them to a slightly different format as they are passed to FaceBook Messenger as described in the code below. Wit only exposes 'msg' and 'quickreplies.' Can you suggest a good way to do this? It goes after "console.log(element)" as far as I understand.
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'));
在上面的示例中,最终结果应为:
In the above example, the end result should be this:
"recipient":{
"id":"USER_ID"
},
"message":{
"text":"Pick a color:",
"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"
}
]
}
我不确定这是否引起混淆,但是没有"JSON对象"之类的东西.一个对象使用JSON.parse
返回的数据对象的方式与处理任何其他对象的方式相同.当然,在发送到FB之前,必须使用JSON.stringify
将数据对象转换为JSON字符串格式. 可能会在某些代码库中自动发生,具体取决于数据的发送方式.
I am not sure if this has been a course of confusion, but there is no such thing as a "JSON object". One works with data objects returned by JSON.parse
in the same manner as working with any other object. Before sending to FB, of course, data objects have to be converted into JSON string format using JSON.stringify
. This might occur automatically in some code libraries depending on how the data is sent.
这是准备quick-replies
数组的示例-我只是为有效负载选择了示例结构并进行了处理. quick_replies
数组仍然是一个对象,尚未转换为JSON字符串.
Here's an example of preparing a quick-replies
array - I simply chose an example structure for the payload and went with it. The quick_replies
array is still an object and has not been converted to a JSON string.
编辑纯文本有效载荷的格式,显示在第一个快速回复的纯文本示例表示有效负载是字符串.下面的代码已更新,可以满足此要求.
Edit the format of a text only payload, shown in the first text only example for quick replies indicates the payload is a string. The code below had been updated to meet with this requirement.
// test values for quickreplies:
var quickreplies= [ "News?", "Subscribe?", "Contribute?", "Organize?" ];
/********
convert quickreplies to quick_replies array
using an example payload of:
{ "text" : "text string", // button text
"index" : index, // index into quickreply for button
"other": "tbd" // anything else needed in a reply
}
*********/
var quick_replies;
if (quickreplies) {
console.log('we got quickreplies, here they are:');
quick_replies = quickreplies.map( function(element, index) {
var payload = {
text: element,
index: index,
other: "tbd" // example value only.
};
var payloadString = JSON.stringify( payload);
console.log(element);
var quick_reply = {
content_type: "text",
title: element,
payload: payloadString
};
console.log("** converted to : " + JSON.stringify(quick_reply));
});
quickreplies=null; // housekeeping
}
else {
console.log('no quickreplies');
quick_replies = undefined; // or [] ?
}