This is a great example of when you want to consider using JSON. Assuming you are developing ASP.NET web pages (as opposed to Web Forms / MVC), create a Javascript.cshtml helper file in your App_Code directory with the following code:
@using System.Web.Script.Serialization;
@helper ToJson(object obj) {
var ser = new JavaScriptSerializer();
@Html.Raw(ser.Serialize(obj))
}
, : @Javascript.ToJson(myObj)
. , - :
@{
var myCSharpObj = new { First = "1st", Second = "2nd" };
}
<script language="javascript">
var myJSObj = @Javascript.ToJson(myCSharpObj);
alert(myJSObj.Second);
</script>