GSON: Expected a string but was BEGIN_OBJECT?

I’m trying to use GSON to parse some very simple JSON. Here’s my code:

    Gson gson = new Gson();

    InputStreamReader reader = new InputStreamReader(getJsonData(url));
    String key = gson.fromJson(reader, String.class);

Here’s the JSON returned from the url:

  • Android material design buttons - Pre lollipop
  • Single black flicker when adding GLSurfaceView
  • Activity with long-lasting service in the background that will not be killed
  • Unity Google Play Services Plugin API is not sync with Show Leaderboard UI function?
  • I want to customize the build process while still using the Android Eclipse ADT plug-in
  • Android: How to prevent the back button from cancelling a FragmentDialog
  • {
      "access_token": "abcdefgh"
    }
    

    I’m getting this exception:

    E/AndroidRuntime(19447): com.google.gson.JsonSyntaxException:     java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 2
    

    Any ideas? I’m new to GSON.

    Related posts:

    How to detect swipe direction between left/right and up/down
    When is View.onMeasure() called?
    TextViews on top and bottom, remaining space filled with ListView
    Dynamically add and remove tabs in TabLayout(material design) android
    How to add extra test runner parameter in Android Studio 2.3 instrumented tests
    Programmatically enable/disable “Battery Saver” mode
  • Tap outside edittext to lose focus
  • Android RatingBar showing more than 5 stars
  • Constructing a RatingBar using images loaded from the web
  • Using Dagger 2 to inject into service
  • Custom Alert dialog not centered vertically on Android
  • Android Audio Latency Workarounds
  • 2 Solutions collect form web for “GSON: Expected a string but was BEGIN_OBJECT?”

    The JSON structure is an object with one element named “access_token” — it’s not just a simple string. It could be deserialized to a matching Java data structure, such as a Map, as follows.

    import java.util.Map;
    
    import com.google.gson.Gson;
    import com.google.gson.reflect.TypeToken;
    
    public class GsonFoo
    {
      public static void main(String[] args)
      {
        String jsonInput = "{\"access_token\": \"abcdefgh\"}";
    
        Map<String, String> map = new Gson().fromJson(jsonInput, new TypeToken<Map<String, String>>() {}.getType());
        String key = map.get("access_token");
        System.out.println(key);
      }
    }
    

    Another common approach is to use a more specific Java data structure that matches the JSON. For example:

    import com.google.gson.Gson;
    import com.google.gson.annotations.SerializedName;
    
    public class GsonFoo
    {
      public static void main(String[] args)
      {
        String jsonInput = "{\"access_token\": \"abcdefgh\"}";
    
        Response response = new Gson().fromJson(jsonInput, Response.class);
        System.out.println(response.key);
      }
    }
    
    class Response
    {
      @SerializedName("access_token")
      String key;
    }
    

    Another “low level” possibility using the Gson JsonParser:

    package stackoverflow.questions.q11571412;
    
    import com.google.gson.*;
    
    public class GsonFooWithParser
    {
      public static void main(String[] args)
      {
        String jsonInput = "{\"access_token\": \"abcdefgh\"}";
    
        JsonElement je = new JsonParser().parse(jsonInput);
    
        String value = je.getAsJsonObject().get("access_token").getAsString();
        System.out.println(value);
      }
    }
    

    If one day you’ll write a custom deserializer, JsonElement will be your best friend.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.