开发者

eval "the serialized object using JavaScriptSerializer()" after removing special characters

开发者 https://www.devze.com 2023-01-01 11:41 出处:网络
I need to eval an JavaScriptSerializer() object. var userS开发者_StackOverflow社区ettings = \'<%= new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(ViewData[\"UserSettings\"]) %

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"); %>';
0

精彩评论

暂无评论...
验证码 换一张
取 消