eval "the serialized object using JavaScriptSerializer()" after removing special characters
I need to eval an JavaScriptSerializer() object.
var userS开发者_StackOverflow社区ettings = '<%= new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(ViewData["UserSettings"]) %>';
The above line gives the output as:
{"SalesTaxPercentage":7.75,"StartOfWeek":"SUNDAY","NoofRowsInList":10,"ShowDashboardEntry":5,"DisplayContactsFormat":"LastName, FirstName","FooterNotes":""When you look good, you feel good...when you feel good, your attitude changes...and attitude is everything!"
You are the heart of my business....THANK YOU!"}
When i use eval for the serialized content like:
userSettings = eval("(" + userSettings + ")");
It throws an error:
missing } after property list
This is because of the special characters in the serialized object (in FooterNotes with " and some other characters in between start and end quotes) during eval.
How can i remove the special characters in serialized before eval?
Or how can i get the value of SalesTaxPercentage
from searialized object?
I think you can simply remove single quotes in the first string and don't use eval (userSettings
will be already an object).
Another way is to double backslashes so your string will stay quoted, something like this (not tested):
var userSettings = '<%= System.Text.RegularExpressions.Regex.Replace(
new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(ViewData["UserSettings"]), @"\134", "\\$0"); %>';
精彩评论