<small id='EJS6C'></small><noframes id='EJS6C'>

<legend id='EJS6C'><style id='EJS6C'><dir id='EJS6C'><q id='EJS6C'></q></dir></style></legend>

<i id='EJS6C'><tr id='EJS6C'><dt id='EJS6C'><q id='EJS6C'><span id='EJS6C'><b id='EJS6C'><form id='EJS6C'><ins id='EJS6C'></ins><ul id='EJS6C'></ul><sub id='EJS6C'></sub></form><legend id='EJS6C'></legend><bdo id='EJS6C'><pre id='EJS6C'><center id='EJS6C'></center></pre></bdo></b><th id='EJS6C'></th></span></q></dt></tr></i><div id='EJS6C'><tfoot id='EJS6C'></tfoot><dl id='EJS6C'><fieldset id='EJS6C'></fieldset></dl></div>

    1. <tfoot id='EJS6C'></tfoot>

        • <bdo id='EJS6C'></bdo><ul id='EJS6C'></ul>

        JavaScript 对象 (JSON) 到 URL 字符串格式

        时间:2023-10-13
        • <bdo id='0SD1T'></bdo><ul id='0SD1T'></ul>

          <small id='0SD1T'></small><noframes id='0SD1T'>

          <legend id='0SD1T'><style id='0SD1T'><dir id='0SD1T'><q id='0SD1T'></q></dir></style></legend>
            <tbody id='0SD1T'></tbody>
                <tfoot id='0SD1T'></tfoot>
                <i id='0SD1T'><tr id='0SD1T'><dt id='0SD1T'><q id='0SD1T'><span id='0SD1T'><b id='0SD1T'><form id='0SD1T'><ins id='0SD1T'></ins><ul id='0SD1T'></ul><sub id='0SD1T'></sub></form><legend id='0SD1T'></legend><bdo id='0SD1T'><pre id='0SD1T'><center id='0SD1T'></center></pre></bdo></b><th id='0SD1T'></th></span></q></dt></tr></i><div id='0SD1T'><tfoot id='0SD1T'></tfoot><dl id='0SD1T'><fieldset id='0SD1T'></fieldset></dl></div>

                  本文介绍了JavaScript 对象 (JSON) 到 URL 字符串格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个类似的 JSON 对象

                  I've got a JSON object that looks something like

                  {
                      "version" : "22",
                      "who: : "234234234234"
                  }
                  

                  我需要将它放在一个准备好作为原始 http 正文请求发送的字符串中.

                  And I need it in a string ready to be sent as a raw http body request.

                  所以我需要它看起来像

                  version=22&who=234324324324
                  

                  但目前我有无数个参数,它需要工作

                  But It needs to work, for an infinite number of paramaters, at the moment I've got

                  app.jsonToRaw = function(object) {
                      var str = "";
                      for (var index in object) str = str + index + "=" + object[index] + "&";
                      return str.substring(0, str.length - 1);
                  };
                  

                  但是在原生 js 中一定有更好的方法来做到这一点?

                  However there must be a better way of doing this in native js?

                  谢谢

                  推荐答案

                  2018年更新

                  var obj = {
                      "version" : "22",
                      "who" : "234234234234"
                  };
                  
                  const queryString = Object.entries(obj).map(([key, value]) => {
                      return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
                  }).join('&');
                  
                  console.log(queryString); // "version=22&who=234234234234"

                  原帖

                  您的解决方案非常好.一个看起来更好的可能是:

                  Your solution is pretty good. One that looks better could be:

                  var obj = {
                      "version" : "22",
                      "who" : "234234234234"
                  };
                  
                  var str = Object.keys(obj).map(function(key){ 
                    return encodeURIComponent(key) + '=' + encodeURIComponent(obj[key]); 
                  }).join('&');
                  
                  console.log(str); //"version=22&who=234234234234"
                  

                  +1 @Pointy 用于 encodeURIComponent

                  +1 @Pointy for encodeURIComponent

                  这篇关于JavaScript 对象 (JSON) 到 URL 字符串格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在 XMLHttpRequest 中获取 SENT 标头 下一篇:为什么在调用 open throw 之前设置 XMLHttpRequest res

                  相关文章

                  最新文章

                  <small id='IScic'></small><noframes id='IScic'>

                    1. <i id='IScic'><tr id='IScic'><dt id='IScic'><q id='IScic'><span id='IScic'><b id='IScic'><form id='IScic'><ins id='IScic'></ins><ul id='IScic'></ul><sub id='IScic'></sub></form><legend id='IScic'></legend><bdo id='IScic'><pre id='IScic'><center id='IScic'></center></pre></bdo></b><th id='IScic'></th></span></q></dt></tr></i><div id='IScic'><tfoot id='IScic'></tfoot><dl id='IScic'><fieldset id='IScic'></fieldset></dl></div>

                        <bdo id='IScic'></bdo><ul id='IScic'></ul>
                    2. <tfoot id='IScic'></tfoot>
                      <legend id='IScic'><style id='IScic'><dir id='IScic'><q id='IScic'></q></dir></style></legend>