Issue
I need to store chart configuration in DB calling API. Next time I get that config as a string from DB and I need to convert it to object type.
For some reason data conversion does not work, and I'm confused why.
The result in console is this:
But actually, result should be like this:
Does someone know some trick on how to convert string to type?
Solution
Finnaly I found the way:
public covertObjectToJSON(obj: string): JSON {
obj = obj.replace(/\/\*[\s\S]*?\*\/|\/\/.*/g, '').trim(); // Strip comments
obj = obj.replace(/'/g, '"'); // Change single quotas with double one
var jsonStr = obj.replace(/(\w+:)|(\w+ :)/g, function (x) {
let isNumber: boolean = !isNaN(parseInt(x));
if (!isNumber)
return '"' + x.substring(0, x.length - 1) + '":';
else return x;
});
return JSON.parse(jsonStr);
}
Answered By - Nemanja Andric
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.