How do I receive JSON formated data into C# method argument
I am using WebKit browser control in my .net application. I am also new to JSON and have little knowledge of Java script. My is to call one method C# method from java script code. I know there is "ObjectForScripting" property with WebKit control.
C# Code:- This code get executed on one of the button click. Which inject java script into rendered HTML page.
Element scriptElm = browser.Document.CreateElement("script");
scriptElm.AppendChild(browser.Document.CreateTextNode("Test(['ABC','EFG'],{'name':'First Name','surname':'LastName'})"));
NodeList headElm = browser.Document.GetElementsByTagName("head");
headElm[0].AppendChild(scriptElm);
Above code calls below Java script method from rendered HTML page.
Java Script code:
function Test(arg1,arg2)
{
document.write(arg2.name); // Printing correct i.e. Firstname
document.write(arg2); // Bad!!!! always print [Object object]
window.external.TestMethod(arg1,arg2); // Call method in C# code.
}
C# Method signature
public bool TestMethod(stri开发者_C百科ng obj1, String obj2)
{
MessageBox.Show(obj1.ToString());
MessageBox.Show(obj2.ToString());
return true;
}
So in above callback method I am not able to get second parameter i.e.obj2 correctly. In C#. I always get it as Object. I tried setting that to Object but didn't work.How should I convert it into right format?
Thanks, Omky
Trying changing your method signature to:
public bool TestMethod(object obj1, object obj2)
Set a breakpoint on it and have a look at the types you are actually receiving. Also, if the breakpoint doesn't hit, you have a different problem.
精彩评论