How to parse JSON response (different object types) with GSON

前端 未结 1 682
广开言路
广开言路 2021-01-22 09:58

Problem: parse the following response from Foursquare Venues API:

{
    meta: {
        code: 200
    }
    notifications: [
    {
        type: \"notificationTr         


        
相关标签:
1条回答
  • 2021-01-22 10:36

    This is correct because the object you are trying to access is not an array, you should do something like this:

    JsonParser parser = new JsonParser();
    JsonObject data = parser.parse(response).getAsJsonObject();
    Meta meta = gson.fromJson(data.get("meta"), Meta.class);
    Response myResponse = gson.fromJson(data.get("response"), Response.class);
    

    Or you can create an object containing 3 classes for the 3 objects and then parse everything through GSON.

    0 讨论(0)
提交回复
热议问题