Reputation: 504
Hi I get "Uncaught SyntaxError: Unexpected token ILLEGAL" error, when I run this code
str += "{'value': "+ response_imdb[count] +",
'color':#"+ colorValue +",
'label': "+ response_labels[count] +"
}";
Thanks.
Upvotes: -1
Views: 79
Reputation: 780724
Javascript doesn't allow line breaks in strings. You heave line breaks after ",
at the end of each line. You should change it to:
str += "{'value': "+ response_imdb[count] +",\n"+
'color':#"+ colorValue +",\n"+
'label': "+ response_labels[count] +",\n"+
}";
But it's almost always wrong to try to create JSON strings by hand. Use a function for it, like JSON.stringify
in Javascript, json_encode
in PHP, etc.
There are some other problems there. If the string is going to be parsed as JSON, it the property names need to be in double quotes, not single quotes. And # + colorValue
needs to be in quotes to be a string.
Upvotes: 2
Reputation: 17351
In JavaScript you cannot have multi-line strings (unless you add a backslash to the end of each line).
Make them multiple strings and concatenate them using +
, like so:
str += "{'value': "+ response_imdb[count] +"," +
"'color':#"+ colorValue +"," +
"'label': "+ response_labels[count] +
"}";
Upvotes: 2
Reputation: 6282
Here is another easy way.
str += JSON.stringify({
value: response_imdb[count],
color: '#' + colorValue,
label: response_labels[count]
});
Upvotes: 2