get JSONObject from javascript through WebView in JavaFX
<p>I am using javscript library called leaflet and leaflet-routing to show maps and calculate routes from a start point to an end. So far so good. To do so, I am using WebView form JavaFX to load a html file that contains the javascript needed. 
This is the javascript needed to do so:</p>

<pre><code>function initMap()
{
     map = L.map('map');
}

function addLayers()
{
        L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
            attribution: '&amp;copy; &lt;a href="http://osm.org/copyright"&gt;OpenStreetMap&lt;/a&gt; contributors'
        }).addTo(map);
}

function calculateRoutingExample()
{
    control = L.Routing.control({
        waypoints: [
            L.latLng(currentPosition.latitud, currentPosition.longitud),
            L.latLng(destination.latitud, destination.longitud)
        ],
        routeWhileDragging: false,
        reverseWaypoints: true,
        showAlternatives: true,
        altLineOptions: {
            styles: [
                {color: 'black', opacity: 0.15, weight: 9},
                {color: 'white', opacity: 0.8, weight: 6},
                {color: 'blue', opacity: 0.5, weight: 2}
            ]
        }
    }).addTo(map);

    L.Routing.errorControl(control).addTo(map);
    //Esconder las indicaciones de texto sobre el mapa
    control.hide();     
}
</code></pre>

<p>As you can see I use a variable call control that stores a bunch of useful information. What I want is to be able to access the route points from java. To do so, on javascript I can get a Json object containing all the information needed. Using the following call, I get a JSON formatted String.
The idea is to get this string in java to be able to transform to a JSON object and then use it in other parts of the app.</p>

<pre><code>JSON.stringify(control._routes[0]);
</code></pre>

<p>Value obtained using this method on a web browser javascript debugging console :</p>

<pre><code>"{"name":"Carrer del Taulat, Carrer de Venero","coordinates":[{"lat":41.399859,"lng":2.206204},{"lat":41.399594,"lng":2.205881},{"lat":41.399546,"lng":2.205821},{"lat":41.399158,"lng":2.205341},{"lat":41.398923,"lng":2.205051},{"lat":41.398597,"lng":2.204657},{"lat":41.398972,"lng":2.204181},{"lat":41.399085,"lng":2.204137},{"lat":41.399127,"lng":2.20405},{"lat":41.399128,"lng":2.203947},{"lat":41.399351,"lng":2.203647},{"lat":41.399772,"lng":2.203082},{"lat":41.399837,"lng":2.203096},{"lat":41.3999,"lng":2.203073},{"lat":41.399958,"lng":2.203004},{"lat":41.399983,"lng":2.202891},{"lat":41.400024,"lng":2.20277},{"lat":41.400668,"lng":2.201952},{"lat":41.400731,"lng":2.20196},{"lat":41.400825,"lng":2.201939},{"lat":41.401106,"lng":2.202296},{"lat":41.402006,"lng":2.20112},{"lat":41.40175,"lng":2.20079},{"lat":41.401697,"lng":2.200763},{"lat":41.40159,"lng":2.200775},{"lat":41.401529,"lng":2.200777},{"lat":41.401495,"lng":2.200734},{"lat":41.401447,"lng":2.200652},{"lat":41.401086,"lng":2.201118}],"instructions":[{"type":"Straight","distance":191,"time":24,"road":"Carrer del Taulat","direction":"SW","index":0},{"type":"Right","distance":58,"time":11,"road":"Rambla del Poblenou","direction":"NW","index":5},{"type":"Straight","distance":13,"time":2,"road":"","direction":"N","index":6},{"type":"SlightLeft","distance":17,"time":2,"road":"","direction":"NW","index":7},{"type":"Right","distance":102,"time":20,"road":"Rambla del Poblenou","direction":"NW","index":9},{"type":"Straight","distance":33,"time":4,"road":"","direction":"N","index":11},{"type":"SlightRight","distance":110,"time":21,"road":"Rambla del Poblenou","direction":"W","index":15},{"type":"Straight","distance":18,"time":2,"road":"","direction":"N","index":17},{"type":"Straight","distance":43,"time":6,"road":"Carrer de Llull","direction":"NE","index":19},{"type":"Left","distance":140,"time":17,"road":"carrer de Venero","direction":"NW","index":20},{"type":"Straight","distance":40,"time":5,"road":"Carrer de Pujades","direction":"SW","index":21},{"type":"SlightLeft","distance":39,"time":6,"road":"","direction":"S","index":22},{"type":"Left","distance":56,"time":10,"road":"Rambla del Poblenou","direction":"SE","index":27},{"type":"DestinationReached","distance":0,"time":0,"road":"","direction":"N","index":28}],"summary":{"totalDistance":860,"totalTime":129},"inputWaypoints":[{"options":{"allowUTurn":false},"latLng":{"lat":41.4,"lng":2.206},"_initHooksCalled":true},{"options":{"allowUTurn":false},"latLng":{"lat":41.401,"lng":2.201},"_initHooksCalled":true}],"waypoints":[{"options":{"allowUTurn":false},"latLng":{"lat":41.399859,"lng":2.206204},"_initHooksCalled":true},{"options":{"allowUTurn":false},"latLng":{"lat":41.401086,"lng":2.201118},"_initHooksCalled":true}],"waypointIndices":[0,28],"routesIndex":0}"
</code></pre>

<p>From Java it is possible to call javascript functions using this call:</p>

<pre><code>WebEngine engine = webView.getEngine();
String JSONRoute = engine.executeScript("getJSONRoute()");
</code></pre>

<p>on the javascript side:</p>

<pre><code>function getJSONRoute()
{
    return JSON.stringify(control._routes[0]);
}
</code></pre>

<p>Theoretically, the function on javascript returns a String to Java where later on I will be able to use it. I've tried this with example strings like "Hi I am the message" and it works. </p>

<p>However when I do this, I get this error:</p>

<pre><code>Error getting JSON route : TypeError: undefined is not an object (evaluating 'control._routes[0]')
</code></pre>

<p>How can I get this JSON returned from getJSONRoute() in javascript in my Java APP?</p>

<p>thank you!</p>