You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用谷歌地图Places API获取路线时遇JSON解析错误求助

Fixing "Expected a string but was BEGIN_OBJECT" in Google Maps Directions API Call

Hey there, let's figure out why you're hitting this error when working with the Google Maps Directions API. That error message spells out exactly what's wrong—your code is trying to parse the API response as a plain string, but the API sends back a JSON object instead, so the parser throws an error when it encounters an object where it expected text.

Let's walk through the fixes step by step:

  1. First, verify your API request URL is complete and valid
    From your code snippet, it looks like your requestApi string got truncated at origin="+currentPo...—make sure you're properly:

    • Including all required parameters: origin, destination, and your API key are mandatory for the Directions API.
    • Converting your LatLng coordinates to plain strings (don't pass the LatLng object directly into the URL string).
    • Test the full URL by pasting it into a browser to see the actual JSON response—it'll be an object with fields like status, routes, and legs, not a single string.
  2. Fix your JSON parsing logic
    The mistake here is almost certainly in how you're handling the API response. For example, if you're using a library like Gson, you might have code that looks like this (which is incorrect):

    // ❌ Wrong: Trying to parse a JSON object as a String
    String response = new Gson().fromJson(apiResponseBody, String.class);
    

    Instead, you need to create Java classes that match the structure of the Directions API response, then parse into those classes. Here's a simplified example:

    // Define classes that map to the API's JSON structure
    public class DirectionResponse {
        private String status;
        private List<Route> routes;
        // Add getters and setters
    }
    
    public class Route {
        private List<Leg> legs;
        // Add other fields like overview_polyline, plus getters/setters
    }
    
    public class Leg {
        private Distance distance;
        private Duration duration;
        private List<Step> steps;
        // Getters and setters
    }
    
    // ✅ Correct: Parse the response into your custom object
    DirectionResponse directionData = new Gson().fromJson(apiResponseBody, DirectionResponse.class);
    
  3. Check the API response status
    Before parsing, make sure the API returns a status of OK (you can get this from the parsed object or raw JSON). If you get statuses like INVALID_REQUEST or OVER_QUERY_LIMIT, that means your request has issues (missing parameters, wrong API key, etc.)—and those error responses are also JSON objects, not strings, which would trigger the same error if you try to parse them as text.

Quick recap:

The core issue is a mismatch between what your code expects (a string) and what the API actually sends (a JSON object). By adjusting your parsing logic to use structured classes that mirror the API's response, this error should disappear.

内容的提问来源于stack exchange,提问作者kamakadze

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 04:02:48