JavaScriptSerializer 用于序列化 JavaScript 函数名称

发布于 2024-09-19 07:16:51 字数 607 浏览 11 评论 0原文

我在项目中使用 Flexigrid 在网格工具栏上添加一个按钮,我可以使用如下代码:

...    
"buttons":[
    {"name":"Modifica","bclass":"edit","onpress":"doCommand"},
    {"name":"Elimina","bclass":"delete","onpress":"doCommand"}
],
...

无论如何,“onpress”属性应包含对 js 回调的引用,因此该字段不应包含在引号内。

我正在使用 JavaScriptSerializer 类(在 System.Web.Script.Serialization 命名空间中)来进行序列化。

我必须如何声明变量才能使 JavaScriptSerializer 像这样序列化?

...    
"buttons":[
    {"name":"Modifica","bclass":"edit","onpress":doCommand},
    {"name":"Elimina","bclass":"delete","onpress":doCommand}
],
...

感谢您的帮助!

I am using Flexigrid in my project to add a button on the grid toolbar I can use code like this:

...    
"buttons":[
    {"name":"Modifica","bclass":"edit","onpress":"doCommand"},
    {"name":"Elimina","bclass":"delete","onpress":"doCommand"}
],
...

Anyway the "onpress" attribute shall contain a reference to a js callback and so this field shall not be enclosed within quotation marks.

I am using the class JavaScriptSerializer (in the System.Web.Script.Serialization namespace) to do the serialization.

How I have to declare the variable to make JavaScriptSerializer serialize like this?

...    
"buttons":[
    {"name":"Modifica","bclass":"edit","onpress":doCommand},
    {"name":"Elimina","bclass":"delete","onpress":doCommand}
],
...

Thanks for helping!

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

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

发布评论

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

评论(4

山有枢 2024-09-26 07:16:51

这招怎么样?

var param = new Dictionary<string,string>();

param["onpress"] = "%doCommand%";

return new JavaScriptSerializer().Serialize( param ).Replace( "\"%", "" ).Replace( "%\"", "" );

How about this trick ?

var param = new Dictionary<string,string>();

param["onpress"] = "%doCommand%";

return new JavaScriptSerializer().Serialize( param ).Replace( "\"%", "" ).Replace( "%\"", "" );
み格子的夏天 2024-09-26 07:16:51

据我所知,没有一种使用 .NET 序列化程序执行此操作的本机方法,但您可以在客户端执行以下代码,将 onpress JSON 字符串转换为回调...

var buttons = []; // Your JSON array
for(var i = 0; i < buttons.length; i++) {
  var b = buttons[i];
  b.onpress = window[b.onpress];
}

To the best of my knowledge, there is not a native way to do this with the .NET serializer, but you could execute the following code on the client-side to turn the onpress JSON string into a callback...

var buttons = []; // Your JSON array
for(var i = 0; i < buttons.length; i++) {
  var b = buttons[i];
  b.onpress = window[b.onpress];
}
最美的太阳 2024-09-26 07:16:51

JavaScriptSerializer旨在序列化为JSON,而JSON不能表示函数;它也不能引用先前定义的变量。所以我不相信这是可能的。

JavaScriptSerializer is meant for serializing to JSON, and JSON can not represent functions; it also can't refer to previously defined variables. So I don't believe this is possible.

强辩 2024-09-26 07:16:51

您可以将函数名称存储为字符串

...    
"buttons":[
    {"name":"Modifica","bclass":"edit","onpress":"doCommand"},
    {"name":"Elimina","bclass":"delete","onpress":"doCommand"}
],
...

You can store the function name as a string

...    
"buttons":[
    {"name":"Modifica","bclass":"edit","onpress":"doCommand"},
    {"name":"Elimina","bclass":"delete","onpress":"doCommand"}
],
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文