eval “使用 JavaScriptSerializer() 的序列化对象”删除特殊字符后
我需要评估 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您可以简单地删除第一个字符串中的单引号并且不使用 eval (
userSettings
将已经是一个对象)。另一种方法是使用双反斜杠,这样你的字符串就会保持引号,如下所示(未测试):
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):