【问题标题】:How to convert POST form data to JSON object如何将 POST 表单数据转换为 JSON 对象
【发布时间】:2017-10-16 23:35:06
【问题描述】:

是否有任何默认函数可以将表单数据字符串转换为 json 对象?

这是一个例子

sendNotification=true&type=extended&additionalNotes=&returnToMainPage=true

如您所愿,这是 POST 表单数据的格式。我需要将其转换为 JSON 对象

{
    "sendNotification": "true",
    "type": "extended",
    "additionalNotes": "",
    "returnToMainPage": "true"
}

它也应该像这样处理数组

blog.blogposts[1].comments  1
blog.blogposts[1].likes 12

我想知道如何使用现有的工具和库来做到这一点。我知道我可以编写自己的转换器,但我想应该有一个默认的转换器。

谢谢

重要

我没有表格,我只需要转换表格数据字符串。

【问题讨论】:

标签: javascript json node.js http form-data


【解决方案1】:

Working Demo

// Form Data String
var dataString = "sendNotification=true&type=extended&additionalNotes=&returnToMainPage=true";

// Split the String using String.split() method. It will return the array.
var params = dataString.split("&");

// Create the destination object.
var obj = {};

// iterate the splitted String and assign the key and values into the obj.
for (var i in params) {
  var keys = params[i].split("=");
  obj[keys[0]] = keys[1];
}

console.log(obj); // Object {sendNotification: "true", type: "extended", additionalNotes: "", returnToMainPage: "true"}

【讨论】:

    【解决方案2】:

    我是这样看的

    getStringJson('sendNotification=true&type=extended&additionalNotes=&returnToMainPage=true');
    
    function getStringJson(text) {
        var json = {}, text = text.split("&");
        for (let i in text){
            let box = text[i].split("=");
            json[box[0]] = box[1];
        }
        return JSON.stringify(json);
    }
    

    生成的输出:

    "{"sendNotification":"true","type":"extended","additionalNotes":"","returnToMainPage":"true"}"
    

    【讨论】:

      【解决方案3】:

      基于 Prashanth Reddy 的回答,如果您想要 json 字符串输出,只需在返回时添加 JSON.stringify(myJson);

      var params = getUrlVars('sendNotification=true&type=extended&additionalNotes=&returnToMainPage=true');
      console.log(params);
      function getUrlVars(url) {
          var hash;
          var myJson = {};
          var hashes = url.slice(url.indexOf('?') + 1).split('&');
          for (var i = 0; i < hashes.length; i++) {
              hash = hashes[i].split('=');
              myJson[hash[0]] = hash[1];
          }
          return JSON.stringify(myJson);
      }
      

      输出:{"sendNotification":"true","type":"extended","additionalNotes":"","returnToMainPage":"true"}

      【讨论】:

        【解决方案4】:

        试试这个

        var params = getUrlVars('some=params&over=here');
        console.log(params);
        
        function getUrlVars(url) {
            var hash;
            var myJson = {};
            var hashes = url.slice(url.indexOf('?') + 1).split('&');
            for (var i = 0; i < hashes.length; i++) {
                hash = hashes[i].split('=');
                myJson[hash[0]] = hash[1];
            }
            return myJson;
        }
        

        我在这里找到它Convert URL to json

        【讨论】:

          猜你喜欢
          • 2012-10-09
          • 1970-01-01
          • 2011-10-26
          • 2021-11-19
          • 1970-01-01
          • 2020-12-19
          • 2021-05-26
          • 1970-01-01
          • 2014-11-29
          相关资源
          最近更新 更多