how to build json array dynamically in javascript

风格不统一 提交于 2019-12-20 06:28:32

问题


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"

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"
      }
    ]
  }

回答1:


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.

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 [] ?
}


来源:https://stackoverflow.com/questions/42057925/how-to-build-json-array-dynamically-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!