javascript convert string to object code example

Example 1: convert string to object javascript

var mystr = '{ "hello":"world" }' // NB: Has enclosing ""
var myobj = JSON.parse(mystr);

Example 2: json_decode javascript

JSON.parse(jsonToDecode)

Example 3: js string to object

JSON.parse()

Example 4: js string to node

function createElementFromHTML(htmlString) {
  var div = document.createElement('div');
  div.innerHTML = htmlString.trim();

  // Change this to div.childNodes to support multiple top-level nodes
  return div.firstChild; 
}

Example 5: convert string to object javascript

str = "firstName:name1, lastName:last1"; // NB: No enclosing ""
obj = eval('({' + str + '})');

Tags:

Php Example