Reputation: 19
I've a table in a SQL DB in which I store a JavaScript object like this:
{content: ['First paragraph','second paragraph']}
I get it from DB and try to pass to a function which needs an object formatted like it:
this._schedaService.getObjectFromDBToPrintPdf().subscribe(data => {
pdfMake.createPdf(data).download('tempPdf.pdf');
});
The problem is that data
is always a string.
I've tried JSON.parse(data)
but (obviously) doesn't work.
If I write
cont temp = {content: ['First paragraph','second paragraph']};
it works.
Does anyone have a good idea?
Upvotes: 0
Views: 633
Reputation: 19
I solved it in a tricky way: since JSON.parse
is a specific type of the eval
function, I just focused on JSON. The only solution (that I've found 'till now) is:
var str = "{content: ['First paragraph','second paragraph']}";
var obj = eval("(" + str + ")");
N.B. typeof(obj)
returns
object
Here is an useful link.
Upvotes: 1
Reputation: 5201
If you use the JSON.parse
JavaScript function, e.g.
var data = JSON.parse("{content: ['First paragraph','second paragraph']}");
you will receive the following error:
Uncaught SyntaxError: Unexpected token c in JSON at position 1
because your input string does not have a valid JSON object syntax.
Your input string should have this format:
'{"content": ["First paragraph","second paragraph"]}'
Upvotes: 1