eval “使用 JavaScriptSerializer() 的序列化对象”删除特殊字符后

发布于 2024-09-02 21:01:47 字数 878 浏览 7 评论 0原文

我需要评估 JavaScriptSerializer() 对象。

var userSettings = '<%= new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(ViewData["UserSettings"]) %>';

上面的行给出的输出为:

{"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!"}

当我对序列化内容使用 eval 时,例如:

userSettings = eval("(" + userSettings + ")"); 

它会抛出错误:

属性列表后缺少 }

这是因为在 eval 期间序列化对象中存在特殊字符(在 FooterNotes 中带有 " 以及开始和结束引号之间的一些其他字符)。

我如何在 eval 之前删除序列化中的特殊字符?

或者如何删除我从序列化对象中获取 SalesTaxPercentage 的值?

I need to eval an JavaScriptSerializer() object.

var userSettings = '<%= 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光与爱终年不遇 2024-09-09 21:01:47

我认为您可以简单地删除第一个字符串中的单引号并且不使用 eval (userSettings 将已经是一个对象)。

另一种方法是使用双反斜杠,这样你的字符串就会保持引号,如下所示(未测试):

var userSettings = '<%= System.Text.RegularExpressions.Regex.Replace(
    new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(ViewData["UserSettings"]), @"\134", "\\$0"); %>';

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"); %>';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文