GSON: Expected a string but was BEGIN_OBJECT?

前端 未结 3 447
迷失自我
迷失自我 2020-12-17 10:02

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

    Gson gson = new Gson();

    InputStreamReader reader = new InputStreamReader(ge         


        
3条回答
  •  执笔经年
    2020-12-17 10:53

    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.

提交回复
热议问题