0

I have a web page, when on button click returning javascript object.

<input type="button" value="JSON Object" onclick="JSONObjectTest(user1)" var user1 = { "name" : "myname", "title": "mytitle", "salary" : null};

In C# application, I am loading this web page which contains the above code using web browser. And implemented the below code in C#

   this.webBrowser.ObjectForScripting = new JavascriptEventHandler();

This code in DocumentCompleted

    var scriptBlock = webBrowser.Document.CreateElement("script");

        var sb = new StringBuilder();


        sb.Append("window.JSONObjectTest=function(param){window.external.JSONObjectEvent(param);}");


        scriptBlock.SetAttribute("type", "text/javascript");
        scriptBlock.SetAttribute("text", sb.ToString());
        webBrowser.Document.Body.AppendChild(scriptBlock);

This is code for JavascriptEventHandler(ObjectForScripting) Class

  [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    [ComVisible(true)]
    public class JavascriptEventHandler
    {

        public void JSONObjectEvent(object param)
        {             

         }
     }

I was able to load the web page perfectly. And when a button is clicked on webpage I was able to hit this method - JSONObjectEvent in C#.

My problem here is, I want to deserialize the javascript object into c# class. I am getting object type as System._COMObject.

How to deserialize it? Please help.

Using Newtosnsoft.Json to deserailize.

NOTE : I will be getting javascript object only not in string. If I pass JSON.Stringify(user1) from web page, I can able to deserialize it. But if it is javascript object I cannot able to deserialize.

7
  • window.external.JSONObjectEvent(JSON.stringfy(param)) send string to c# and use that string to deserialize. Commented Sep 11, 2017 at 14:34
  • I'd start by getting rid of the the ComVisbile attribute. Also, is this an MVC website? If so you should be creating a model and let the model converter do this for you. Commented Sep 11, 2017 at 14:52
  • @Kell.. If we did not set COM visible we cannot set ObjectforScripting Class. and we cannot call C# class code. Commented Sep 11, 2017 at 14:56
  • @LB. Tried. Getting JSON as undefined. Commented Sep 11, 2017 at 14:57
  • Ah, it escaped me that you were using an embedded browser. This may help: stackoverflow.com/questions/21120192/… Commented Sep 11, 2017 at 15:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.