Skip to content

Instantly share code, notes, and snippets.

@bennyzhao
Created August 30, 2013 05:49
Show Gist options
  • Save bennyzhao/6386663 to your computer and use it in GitHub Desktop.
Save bennyzhao/6386663 to your computer and use it in GitHub Desktop.
Querystring into a JSON object using JavaScript
function QueryStringToJSON() {
var pairs = location.search.slice(1).split('&');
var result = {};
pairs.forEach(function(pair) {
pair = pair.split('=');
result[pair[0]] = decodeURIComponent(pair[1] || '');
});
return JSON.parse(JSON.stringify(result));
}
// test
/*
* var qs = QueryStringToJSON();
* document.getElementById('json').innerText = JSON.stringify(qs);
*
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment