I have an anonymous type in an ASP.NET web form that I’m trying to expose to client-side script:
var thing = new
{
red = other.Red,
green = other.Green,
blue = other.Blue,
};
JavaScriptSerializer serializer = new JavaScriptSerializer();
Page.ClientScript.RegisterClientScriptBlock(thing.GetType(), "Thing", serializer.Serialize(thing), true);
Firebug shows an “invalid label” error on the third line in the generated script:
<script type="text/javascript">
//<![CDATA[
{"red":null,"green":null,"blue":null}//]]>
</script>
I get the impression that the double quotes are the problem, but I don’t understand why. I looked at the example page on JSON.org, and it seems like that should be valid. What am I doing wrong?
You have to set that JSON object to a variable or pass it as a function argument, example of both below: