Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

string object to object in js

var obj = '{ firstName:"John", lastName:"Doe" }';

var jsonStr = obj.replace(/(w+:)|(w+ :)/g, function(matchedStr) {
  return '"' + matchedStr.substring(0, matchedStr.length - 1) + '":';
});

obj = JSON.parse(jsonStr); //converts to a regular object

console.log(obj.firstName); // expected output: John
console.log(obj.lastName); // expected output: Doe
 Run code snippet
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #string #object #object #js
ADD COMMENT
Topic
Name
7+8 =