Note: Advice in this article will only work for DotNetBrowser 1.
See the corresponding article for DotNetBrowser 2 here.
DotNetBrowser JS - .NET Bridge API allows passing a string that represents JSON from .NET side to JavaScript. On JavaScript side, this JSON string will be parsed and converted to appropriate JavaScript object/objects. The following simple example demonstrates how to associate JSON with the specific property of window JavaScript object:
C#
JSValue window = browser.ExecuteJavaScriptAndReturnValue("window"); window.AsObject().SetProperty("myObject", new JSONString("[123, \"Hello\"]"));
VB.NET
Dim window As JSValue = browser.ExecuteJavaScriptAndReturnValue("window") window.AsObject().SetProperty("myObject", new JSONString("[123, ""Hello""]"))
In the code above the "[123, \"Hello\"]"
JSON string is associated with thewindow.myObject
property. This JSON string will be converted to appropriate JavaScript object. In this case, it will be converted to an array. The following JavaScript code shows how to work with the window.myObject
property:
<script> var length = window.myObject.length; var numberValue = window.myObject[0]; var stringValue = window.myObject[1]; </script>
If you set JSONString with the string value that doesn't represent JSON, you will get JavaScript error.
From version 1.12 it is possible to convert JSObject
into JSON string using JSObject.ToJSONString()
method. It returns a string equivalent to JavaScript JSON.stringify()
method.
C#
JSObject myObj= (JSObject)browser.ExecuteJavaScriptAndReturnValue("myObject"); string jsonString = myObj.ToJSONString();
VB.NET
Dim myObj As JSObject = CType(browser.ExecuteJavaScriptAndReturnValue("myObject"), JSObject) Dim jsonString As String = myObj.ToJSONString()