I have a string like this:
abc=foo&def=%5Basf%5D&xyz=5
How can I convert it into a JavaScript object like this?
{
abc: 'foo',
def: '[asf]',
xyz: 5
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In the year 2021… Please consider this obsolete.
Edit
This edit improves and explains the answer based on the comments.
Example
Parse
abc=foo&def=%5Basf%5D&xyz=5in five steps:abc=foo","def=[asf]","xyz=5abc":"foo","def":"[asf]","xyz":"5{"abc":"foo","def":"[asf]","xyz":"5"}which is legal JSON.
An improved solution allows for more characters in the search string. It uses a reviver function for URI decoding:
Example
gives
Original answer
A one-liner: