Welcome Guest Search | Active Topics | Sign In | Register

Passing JSON from .net c# to Javascript Options
Sven Dhaens
Posted: Wednesday, May 20, 2015 10:14:21 AM
Rank: Member
Groups: Member

Joined: 5/20/2015
Posts: 11
Hi,

I just started using EO.WebBrowser, I am using Webforms and c#
(Need to implement googlemaps.api replacing the curent mapping system)

I need to pass some JSON Objects to Javascript to work with.
Used Newtonsoft.Json to create the objects and now am looking to pass em on to the EO.webview containing a javascript-function like so.

////////
public void lookupAddress(string address)
{
string temp = address;
Location loc = coder.GeocodeAddress(temp);

int zoomlevel = coder.calcZoom(loc);

JObject rss =
new JObject(
new JProperty("lat", loc.getLatitude().ToString().Replace(",", ".")),
new JProperty("longt", loc.getLongitude().ToString().Replace(",", ".")),
new JProperty("zoomlevel", zoomlevel),
new JProperty("description", loc.getAddressFull()),
new JProperty("imagepath", crossImagePath));

EO.WebBrowser.JSObject window = EO_WebView.GetDOMWindow();
//Get the JavaScript function
EO.WebBrowser.JSFunction jsAddMarker = (EO.WebBrowser.JSFunction)EO_WebView.EvalScript("RSSAddMarkers");
//Call the function
jsAddMarker.Invoke(window, new Object[] { rss });
}

//////////////////////////

When running the application visual studio gives me the folowing when trying to execute the invoke():
Object '{ .... } of type 'Newtonsoft.Json.Linq.JObject' can not be passed to JavaScript code.

Should I be working with something else to get somekind of JSON-objects through??

I probably missed something, but if anyone could give me some tips on handling this that would be awesome.
eo_support
Posted: Thursday, May 21, 2015 11:22:30 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,071
Hi,

You can not pass a .NET object directly into the JavaScript excepts for primative types (theorartically we could automatically convert a .NET object into a JavaScript object under the hood but that would come with a performance penalty). Instead of passing JObject to the JavaScript, you can serialize it into a JSON string and then pass that string into JavaScript. You can then parse it back into JSON object with the EO.WebBrowser's built in JSON support in your JavaScript:

Code: JavaScript
//Parse JSON string back to an object in JavaScript
var jobj = JSON.parse(your_json_string);


Note here JSON is the built-in JSON support provided by the browser engine. This is also much more efficient.

Thanks!
Sven Dhaens
Posted: Friday, May 22, 2015 4:27:18 AM
Rank: Member
Groups: Member

Joined: 5/20/2015
Posts: 11
Works like a charm! Thanks for the help!


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.