2017-05-17 2 views
0

s'il existe des fonctions par défaut qui peuvent convertir une chaîne de données post-formulaire en objet json?Comment convertir les données de formulaire POST en objet JSON

Voici un exemple

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

Comme vous pouvez c'est le format des données de formulaire POST. Je dois le convertir en objet JSON

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

En outre, il doit gérer des tableaux comme celui-ci

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

Je me demande comment je peux le faire en utilisant les outils et les bibliothèques existantes. Je sais que je peux écrire mon propre convertisseur, mais je suppose qu'il devrait y en avoir un par défaut.

Merci

IMPORTANT

Je n'ai pas une forme, je dois convertir simplement la chaîne de données de formulaire.

+0

Vous pouvez sérialiser la fo données RM. Regardez ici: http://stackoverflow.com/questions/11338774/serialize-form-data-to-json –

+0

Veuillez cocher la case si la réponse vous a aidé. –

Répondre

2

Essayez cette

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; 
} 

je l'ai trouvé ici Convert URL to json

1

Miser sur la réponse de Prashanth Reddy, si vous voulez la sortie de chaîne JSON simplement ajouter JSON.stringify(myJson); sur le retour

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); 
} 

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

0

Je vois cette façon

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); 
} 

sortie générée:

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

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