把json格式對象轉成可提交字串格式,會過濾掉函數 {a: {b: 3}, b: [1], c: "d"} -> a.b=3&b[0]=1&c=d

來源:互聯網
上載者:User

標籤:des   style   blog   color   io   os   ar   for   div   

var json = {       name: "任務名稱" ,       scoreRule: "",       score: "",           //     如果規則運算式不為空白,則預設選中 “按規則運算式計算”       unique: 1,       startTime: "2014-09-15 20:20:20",       endTime: "2014-10-15 20:20:20",       status: 1,       isTaks: 0,       tradeType: 1,       description: "業務描述" ,       codes: [ "16", "6" ],  // 選中的平台       ids: [ "3"],                        // app內部連結       test: {              a: 1,              b: [1, 2, 3]       }};var json2 = {       a: 1,       b: 2,       c: {              a: 1,              b: {                     a: 1,                     b: [1, 2]              },              c: [1, 2]       },       d: [1, 2, {              a: 1,              b: {                     a: 1              },              c: [1, 2]       }]};/** * 把json格式對象轉成可提交字串格式,會過濾掉函數 {a: {b: 3}, b: [1], c: "d"} -> a.b=3&b[0]=1&c=d * @param  {Object} data   要轉化的json對象 * @param  {String} prefix 要帶的首碼 * @return {String}        字串 */function paramData(data, prefix) {        var _getType = ({}).toString,              _paramArray = function(arr, prefix) {                      var    result = [],                           j = 0,                           len = arr.length;                      for (; j < len; j++) {                            var data = arr[j],                                   type = _getType.call(data),                                   subPrefix = prefix + "[" + j + "]";                           result = result.concat(_paramAll(arr[j], prefix + "[" + j + "]"));                     }                      return result;              },              _paramAll = function(data, prefix) {                      var result = [],                           type = _getType.call(data);                      switch (type) {                            case "[object Object]" :                                   var subPrefix = prefix ? prefix + "." : "",                                                                 i;                                   for (i in data) result = result.concat(_paramAll(data[i], subPrefix + i));                                   break;                            case "[object Array]" :                                  result = result.concat(_paramArray(data, prefix));                                   break;                            case "[object String]" :                            case "[object Number]" :                                  result.push(prefix + "=" + data);                     }                      return result;              };       prefix = prefix || "";        // if (prefix) {     // prefix必須以 . 結尾        //     prefix = /\.$/.test(prefix) ? prefix : (prefix + ".");        // } else {        //     prefix = "";        // }        return (function () {               return _paramAll(data, prefix);       }()).join( "&");      }console.log(paramData(json2, "rule"));console.log(paramData(json, "rule"));

 

把json格式對象轉成可提交字串格式,會過濾掉函數 {a: {b: 3}, b: [1], c: "d"} -> a.b=3&b[0]=1&c=d

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.