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.

Swetha Bindu
  • 639
  • 2
  • 9
  • 29
  • `window.external.JSONObjectEvent(JSON.stringfy(param))` send string to c# and use that string to deserialize. – L.B Sep 11 '17 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. – Kell Sep 11 '17 at 14:52
  • @Kell.. If we did not set COM visible we cannot set ObjectforScripting Class. and we cannot call C# class code. – Swetha Bindu Sep 11 '17 at 14:56
  • @LB. Tried. Getting JSON as undefined. – Swetha Bindu Sep 11 '17 at 14:57
  • Ah, it escaped me that you were using an embedded browser. This may help: https://stackoverflow.com/questions/21120192/passing-a-c-sharp-class-instance-back-to-managed-code-from-javascript-via-com – Kell Sep 11 '17 at 15:20
  • @SwethaBindu https://stackoverflow.com/questions/2503175/json-on-ie6-ie7 – L.B Sep 11 '17 at 16:37
  • @LB - sorry I missed the previous comment- send string to c# and use that string to deserialize. - I cannot send string to C#. It will javascript object only. – Swetha Bindu Sep 12 '17 at 09:34

0 Answers0