Quantcast
Channel: Xojo Programming Forum - Latest topics
Viewing all articles
Browse latest Browse all 3949

JSonItem cast issue

$
0
0

Hello all;

I have a method that works perfectly fine in the IDE, but fails once compiled. The error is

_VariantString cannot be cast to JSONItem

Here is the method:

// This method will get a distance from the Google Routes API. 

Var LoSource As String = Officiel.Longitude
Var LaSource As String = Officiel.latitude
Var LoDest As String = Site.Longitude
Var LaDest As String = Site.latitude

Var GetDistance as New URLConnection
Var Request as string = "{""origin"":{""location"":{""latLng"":{""latitude"": " + LaSource + ",""longitude"": " + LoSource + "}}},""destination"":{""location"":{""latLng"":{""latitude"":" + LaDest +",""longitude"":" + LoDest + "}}},""travelMode"": ""DRIVE"",""routingPreference"": ""TRAFFIC_UNAWARE"",""computeAlternativeRoutes"": false,""routeModifiers"": {""avoidTolls"": false,""avoidHighways"": false,""avoidFerries"": false},""languageCode"": ""fr-CA"",""units"": ""METRIC""}"

GetDistance.RequestHeader("X-Goog-Api-Key") = APIKeyG.left(APIKeyG.length-2)
GetDistance.RequestHeader("X-Goog-FieldMask") = "routes.distanceMeters"
GetDistance.SetRequestContent(Request, "application/json")
Var Response As String = GetDistance.SendSync("POST", "https://routes.googleapis.com/directions/v2:computeRoutes", 5)
'system.debuglog Response

Var Resp as New JSONItem 
Resp.Load(Response)
Var RespRoutes as New JSONItem 
RespRoutes = Resp.lookup("routes", "Erreur")
Var Route as New JSONItem
Route = RespRoutes.Valueat(0)               ' always select the default (preferred) route

Var Dist as Variant
Dist = Route.value("distanceMeters")

Return Dist.IntegerValue
'System.debuglog Dist.stringvalue

Is this something I am doing wrong, or did I stumble on a bug? It puzzles me somewhat that the same method works fine in the IDE, and fails once compiled.

I am developing on Windows 11, but compiling for Linux X64. This is with 2024 R4.1.

All insights are welcome. Thank you in advance.

LD

13 posts - 3 participants

Read full topic


Viewing all articles
Browse latest Browse all 3949

Trending Articles