[英]How to convert POST form data to JSON object
if there any default functions that can convert a post form data string into json object ? 是否有任何默认功能可以将邮政表格数据字符串转换为json对象?
Here is an example 这是一个例子
sendNotification=true&type=extended&additionalNotes=&returnToMainPage=true
As you can this is the format of POST form data. 您可以使用POST表单数据的格式。 I need to convert it into JSON object 我需要将其转换为JSON对象
{
"sendNotification": "true",
"type": "extended",
"additionalNotes": "",
"returnToMainPage": "true"
}
Also it should handle arrays like this 它也应该像这样处理数组
blog.blogposts[1].comments 1
blog.blogposts[1].likes 12
I wonder how can I do this using existing tools and libraries. 我想知道如何使用现有工具和库来执行此操作。 I know that I can write my own converter, but I guess there should a default one. 我知道我可以编写自己的转换器,但我想应该有一个默认转换器。
Thanks 谢谢
IMPORTANT 重要
I don't have a form, I need just convert the form data string. 我没有表单,我只需要转换表单数据字符串即可。
Try this 尝试这个
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;
}
I found it here Convert URL to json 我在这里找到它将URL转换为json
Building on the answer from Prashanth Reddy, if you want json string output simply add JSON.stringify(myJson);
基于Prashanth Reddy的答案,如果您想输出json字符串,只需添加JSON.stringify(myJson);
on the return 在返回
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);
}
Output: {"sendNotification":"true","type":"extended","additionalNotes":"","returnToMainPage":"true"}
输出: {"sendNotification":"true","type":"extended","additionalNotes":"","returnToMainPage":"true"}
I see it this way 我这样看
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);
}
Output generated: 产生的输出:
"{"sendNotification":"true","type":"extended","additionalNotes":"","returnToMainPage":"true"}"
// 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"}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.