why is my SERIALIZED JSON getting UN-SERIALIZED after it reaches a java
script function parameter?
I have a java script function that gets executed on the page pre-render
event:
protected void Page_PreRender(object sender, EventArgs e)
{
var script = "<script>CreateProjectTree('" + serializedSearchCriteria
+ "');" + "</script>";
ClientScript.RegisterStartupScript(typeof (string),
"createProjectTree", script);
}
The variable that gets passed to the above java script function is a
serialized JSON that gets populated on the Page_Load event:
serializedSearchCriteria =
JsonConvert.SerializeObject(ProjectSearchCriteria);
This is the value of serializedSearchCriteria after it has been serialized:
{"QueryString":null,"ProjectName":"\"24\"","SeasonName":"","MemberName":"","CompanyName":"","CompanyRole":0,"CompanyRoles":"","Year":""}
You can see that serializing it successfully escapes the double quotes for
the value 24.
The issue here is that when I look at the java script function under debug
mode, the value of the variable serializedSearchCriteria is now this:
{"QueryString":null,"ProjectName":""24"","SeasonName":"","MemberName":"","CompanyName":"","CompanyRole":0,"CompanyRoles":"","Year":""}"
Notice the 24 now. It isn't escaped anymore. Therefore, this object isn't
a valid JSON anymore once it's inside the java script function.
Why is this happening?
The object should be coming in SERIALIZED ... at some point, it is getting
un-serialized. Does anyone know why?
No comments:
Post a Comment