0

I have this string:

ROZ=misparey_batim&CL=rechovot

I need to convert this string:

"{ROZ:'misparey_batim', CL:'rechovot'}"

How can I convert it using javascript or jquery?

0

2 Answers 2

3

var inputstring = "ROZ=misparey_batim&CL=rechovot";
console.log(JSON.parse('{"' + decodeURI(inputstring).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g,'":"') + '"}'));

Sign up to request clarification or add additional context in comments.

2 Comments

This answer is just copied from here: stackoverflow.com/questions/8648892/…
You can't say copied because thats the way. i have already marked it duplicate
1

Simplest approach:

const keyVals = string.split('&');
const results = {};
keyVals.forEach(kv => {
   kv = kv.split('=');
   results[kv[0]] = kv[1];
});

But personally I'd just search for a query string parser Parse query string in JavaScript

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.