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

时间:2018-01-12 11:54:28

标签: javascript c# webbrowser-control

我的HTML文档中有以下javascript函数:

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

jsonArray的示例如下:

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

我按照帖子“Creating an JSON array in C#”中的说明操作,以便在C#中创建JSON数组对象。

从我的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);

然而,它不起作用。 我忘了什么吗?

1 个答案:

答案 0 :(得分:2)

jsFunction是3参数。

function jsFunction(string, jsonArray, string) { ... }
你输了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";

删除此行。

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

解析jsonArray并在jsFunction中使用。

var data = JSON.parse(jsonArray );