Using google geocode API, when i using PHP i can do with this:
$request = file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?latlng=-6.408107,107.468262&key=AIzaSyCUzlhl-ibUJsKCwWDwsZqjiW7EP_On30g&sensor=false");
$json = json_decode($request, true);
echo $json['results'][0]['address_components'][0]['long_name'];
echo $json['results'][0]['address_components'][1]['long_name'];
now, how do it on DELPHI? i try this but got EXCEPTION
myjson:=RESTResponse1.Content;
JSONObject := TJSONObject.ParseJSONValue(myjson) as TJSONObject;
JSONObject2 := JSONObject.GetValue('results') as TJSONObject;
arrayjson := JSONObject2.GetValue('address_components') as TJSONArray;
currcond := arrayjson.Items[0] as TJSONObject;
showmessage(currcond.GetValue('long_name').Value);
The JSON results
element you access in your PHP code is an array type. Yet, in your Delphi code, you're trying to access it as an object. This causes arrayjson
to return nil
, and when you try to access arrayjson
, since it doesn't exist, you get an Access Violation
exception.
Instead, you should read the first results
element as an array. Then, either read the first (0
) element in that array, or iterate through all of them - depending on the need.