I am struggling with parsing a string variable back to an object. Despite searching through various posts on this issue, I have not found a solution that works for me.
if(subMatch.match(/\{.*\}/)){ /// new Object of some sort
var objStr=subMatch.match(/\{.*\}/)[0];
//objStr= JSON.stringify(objStr); // I tried this , no difference
//objStr='"'+objStr+'"'; // Tried this way: unexpected token t
//objStr="'"+objStr+"'"; // Tried this way: unexpected token '
objStr=JSON.parse("'"+objStr+"'"); // puts out unexpected token '
The string in question is:
{"type": "lawnmowing","hours": 10,"rate": 10.5,"permanent": false}
Even though it is valid according to JSONLint, the addition of extra quotes changes its appearance to:
'{"type": "lawnmowing","hours": 10,"rate": 10.5,"permanent": false}'
I came across this question related to JSON.Parse and 'Uncaught SyntaxError: Unexpected token o', but the variables discussed there are objects from the start. Mine is definitely a String. Adding literal single quotes around objStr should not be an issue since it's already a String, right? I've also attempted without the additional quotation marks altogether.
Do you have insights on how to properly use JSON.parse on a String variable? While eval works, it involves user input that needs to be converted into an object.
Your help would be greatly appreciated! Thank you, Jenita