I am having String like this
{Name: India, Path: test.png, Id: 1, Uri: /api/1}
Through Javascript I tried to parse this value like this
var sCountry = document.getElementById("countries").value; // this will give value as {Name: India, Path: test.png, Id: 1, Uri: /api/1}
var fixedJSON = sCountry
// Replace ":" with "@colon@" if it's between double-quotes
.replace(/:\s*"([^"]*)"/g, function(match, p1) {
return ': "' + p1.replace(/:/g, '@colon@') + '"';
})
// Replace ":" with "@colon@" if it's between single-quotes
.replace(/:\s*'([^']*)'/g, function(match, p1) {
return ': "' + p1.replace(/:/g, '@colon@') + '"';
})
// Add double-quotes around any tokens before the remaining ":"
.replace(/(['"])?([a-z0-9A-Z_]+)(['"])?\s*:/g, '"$2": ')
// Turn "@colon@" back into ":"
.replace(/@colon@/g, ':')
;
console.log('Before: ' + sCountry);
console.log('After: ' + fixedJSON);//Output comes like this {"Name": India, "Path": test.png, "Id": 1, "Uri": /api/1}
var obj = JSON.parse(fixedJSON);
It gives error like this
unexpected token e in json at position 10 at json.parse
I guess the output should be like this
{"Name": "India" , "Path": "test.png", "Id": 1, "Uri": "/api/1"}
Can anyone help me to solve this String to JSON conversion. so that I can parse and get the value of "Id"
JSON.parse?