简体   繁体   English

如何将JSON数组传递给C#WebBrowser控件中的javascript函数?

[英]How can I pass a JSON array to a javascript function in the C# WebBrowser Control?

I have following javascript function in my HTML document: 我的HTML文档中有以下javascript函数:

function jsFunction(string, jsonArray, string) { ... }

An example of jsonArray would be following: jsonArray的示例如下:

[
{"name":"foo", "value":"21980"},
{"name":"bar", "value":"100"},
{"name":"foo", "value":"27492328"},
{"name":"bar", "value":"WEB21980001831"}
]

I followed the instructions from the post " Creating an JSON array in C# " in order to create a JSON Array object in C#. 我按照“ 在C#中创建JSON数组一文中的说明进行操作,以便在C#中创建JSON数组对象。

From my Windows Form I should be able to call the JavaScript function like this: 从我的Windows窗体中,我应该能够像这样调用JavaScript函数:

 Object[] jsParams = new Object[3];
 jsParams[0] = (Object)"test";
 jsParams[1] = new
 {
     items = new[] {
         new {name = "foo" , value = "21980"}, 
         new {name = "bar" , value = "100"}, 
         new {name = "foo" , value = "27492328"}, 
         new {name = "bar" , value = "WEB21980001831"}
     }
 };
 jsParams[2] = (Object)"test";

 this.webBrowserCtl.Document.InvokeScript("jsFunction", jsParams);

However, it doesn't work. 但是,它不起作用。 Did I forget something? 我忘记了什么吗?

jsFunction is 3 parameter. jsFunction是3个参数。

function jsFunction(string, jsonArray, string) { ... }

you send 4 argument. 您发送4个参数。

 jsParams[0] = (Object)"test";
 jsParams[1] = new
 {
    items = new[] {
        new {name = "foo" , value = "21980"}, 
        new {name = "bar" , value = "100"}, 
        new {name = "foo" , value = "27492328"}, 
        new {name = "bar" , value = "WEB21980001831"}
    }
 };
 jsParams[2] = (Object)"content";
 jsParams[3] = (Object)"test";

Delete this line. 删除此行。

 //jsParams[3] = (Object)"test";

Parse jsonArray and use in jsFunction. 解析jsonArray并在jsFunction中使用。

var data = JSON.parse(jsonArray ); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM