1

How can i convert this string into object so that i can access it's property like obj.Name ?

{
    Name = Mahbubr Rahman, 
    Gender = Male, 
    Birthday = 1 / 5 / 1992 6: 00: 00 AM, 
    Email = mahbubur.rahman@ rms.com, 
    EmployeeType = Manager
}

I have tried with JSON.parse() and eval but getting nothing. Any help ?

var obj = JSON.parse(
  JSON.stringify('{ Name = Mahbubr Rahman,Gender = Male, Birthday = 1/5/1992 6:00:00 AM, Email = [email protected], EmployeeType = Manager }'.replace(/=/g, ':'))
);
1
  • 3
    Can you not change the format the strings are stored in to be valid JSON? Then you could just use JSON.parse(). Having to hack around a string in to the right format seems to be fixing the wrong part of the problem. Commented Feb 1, 2016 at 11:24

3 Answers 3

1

try this one as well

var str = "{ Name = Mahbubr Rahman,Gender = Male, Birthday = 1/5/1992 6:00:00 AM, Email = [email protected], EmployeeType = Manager }"

var obj = JSON.parse(str.split(/\s*=\s*/).join("\":\"").split(/\s*,\s*/).join("\",\"").split(/{\s*/).join("{\"").split(/\s*}/).join("\"}"));

console.log(obj);

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

Comments

1

You can try something like this:

var str = '{ Name = Mahbubr Rahman,Gender = Male, Birthday = 1/5/1992 6:00:00 AM, Email = [email protected], EmployeeType = Manager }';

str = str.replace(/=/g, '\":\"');
str = str.replace(/,/g, "\", \"");
str = str.replace(/{/g, "{\"");
str = str.replace(/}/g, "\"}");

console.log(str);

var obj = JSON.parse(str);

console.log(obj);

Comments

1

You can do it like following. Wrap all property name and value with double quotes (") and replace = with :.

var st = '{ Name = Mahbubr Rahman, Gender = Male, Birthday = 1/5/1992 6:00:00 AM, Email = [email protected], EmployeeType = Manager }';
st = st.replace(/=/g, '":"');
st = st.replace(/{/g, '{"');
st = st.replace(/}/g, '"}');
st = st.replace(/,/g, '","');

var obj = JSON.parse(st);

console.log(obj);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.