JSON to JavaScript, SyntaxError: Unexpected token & -
i know question has been asked numerous times, don´t it.
i creating site in mvc, , i'm creating json string model. want pass argument javascript function uses plot graph.
here create json string. indeed creates valid json string, checked @ jsonlint.
@{ var serializer = new system.web.script.serialization.javascriptserializer(); var weightsasjsonstring = serializer.serialize(enumerable.select(model, weight => new { date = weight.date, value = weight.value })); }
further down create javascript variable , pass javascript function:
var jsonstringtograph = @weightsasjsonstring; google.setonloadcallback(drawvisualization(jsonstring));
when run this, console prints 'syntaxerror: unexpected token &' @ line declare jsonstringtograph
. googled around , concluded should put ' ' around @weightsasjsonstring
, that.
anyway, in drawvisualization, this:
function drawvisualization(teststring) { ....... var parsedjson = json.parse(teststring);
this gives me syntaxerror: unexpected token & index:1
know code @ bottom causing exception, not understand why. understand doing wrong?
edit: weightsasjsonstring
[{"date":"\/date(1434492000000)\/","value":100.2},{"date":"\/date(1434578400000)\/","value":99.2},{"date":"\/date(1434664800000)\/","value":101.2},{"date":"\/date(1434751200000)\/","value":98.2},{"date":"\/date(1434837600000)\/","value":97.2},{"date":"\/date(1434924000000)\/","value":96.2},{"date":"\/date(1435010400000)\/","value":95.2},{"date":"\/date(1435096800000)\/","value":94.2}]
it sounds issue trying inject content via razor javascript. default @
html-encode content, doesn't work in context of javascript.
@html.raw(weightsasjsonstring)
work better, , js have javascript object, there's no need json.parse
later on.
Comments
Post a Comment