-3

I have a string like this:

abc=foo&chk_seq%5B%5D=5&chk_seq%5B%5D=7&xyz=5

(it comes from ajaxOptions.data parameter from the ajaxComplete event)

How can I convert it into a javascript object like this:

obj = {
  abc: 'foo',
  chk_seq[]: 5,
  chk_seq[]: 7,
  xyz: 5
}
taebu
  • 57
  • 2
  • 8

1 Answers1

0

First Google result:

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

It even has a fiddle!

Update:

I think this is what you may actually be looking for:

First google result for "javascript convert url parameters to object":

var search = location.search.substring(1);
JSON.parse('{"' + decodeURI(search)
    .replace(/"/g, '\\"')
    .replace(/&/g, '","')
    .replace(/=/g,'":"') + '"}'
);
Community
  • 1
  • 1
arielnmz
  • 8,354
  • 9
  • 38
  • 66
  • it's this chk_seq%5B%5D=5& chk_seq%5B%5D=7 this result {abc: "foo", chk_seq%5B%5D: "7", xyz: "5"} – taebu Jun 12 '14 at 07:15
  • That's an URL enconded string of parameters as far as I know. JSFiddle is down so I can't check if this works for arrays too. Check my updated answer. – arielnmz Jun 12 '14 at 07:19
  • i do searching in googling... but all things don't like answer. – taebu Jun 12 '14 at 07:36
  • arielnmz yea i tried the second code, but doesn't not multivalue. result to chk_seq only singlevalue. – taebu Jun 12 '14 at 08:46