Skip to content Skip to sidebar Skip to footer

How Can I Parse An Associative Array Using Gson Converter In Retrofit?

I'm receiving a JSON response from a PHP server. In android, I need to write a java model (POJO) to use to parse the response in Retrofit (An Android library for http-requests). JS

Solution 1:

To parse JSON with dynamic keys, you will need a Map in your POJO class.

Add the following POJO classes to your project:

  1. CalendarResponse.java

    publicclassCalendarResponse {
      @SerializedName("calendar")
      Map<String, List<Entry>> entries;
    
      @SerializedName("error")
      privateboolean error;
    }
    
  2. Entry.java

    publicclassEntry{
      @SerializedName("time")private String time;
    
      @SerializedName("title")private String title;
    
      @SerializedName("description")private String description;
    
      @SerializedName("color")private String color;
    }
    
  3. Use the CalendarResponse class in your retrofit interface for your endpoint, see example below

    publicinterfaceCalendarService {
      @GET("<insert your own relative url>")
      Call<CalendarResponse> listCalendar();
    }
    
  4. Execute the call (synchronously) as follows:

    Call<CalendarResponse>call= calendarService.listCalendar();
    CalendarResponse result= call.execute().body();
    

If needed, here is an example to parse the JSON with GSON:

Gsongson=newGsonBuilder().create();
CalendarResponseb= gson.fromJson(json, CalendarResponse.class);

Solution 2:

Normally, you would create a POJO which is a representation of your JSON, but in this case, you would need a 2016-06-10 class and a 2016-06-11 class.

This isn't a solution. Therefore, change the JSON response to make the date a separate value:

{
  "calendar": [
    {
      "date": "2016-06-10",
      "entries": [
        {
          "time": "10h00m",
          "title": "PROVA P2",
          "description": "LP / RED / ED.FIS - 80 E 90",
          "color": "#990000"
        }
      ]
    }
  ]
}

Better yet, just make one dateTime value and make it a proper ISO 8601 timestamp while you're at it:

{
  "calendar": [
      {
        "time": "2016-06-10T08:00:00.000Z",
        "title": "PROVA P2",
        "description": "LP / RED / ED.FIS - 80 E 90",
        "color": "#990000"
      }
    ]
}

If you have no control over the server serving the JSON then you should use Retrofit to just get a String and do the Gson conversion yourself via gson.

Solution 3:

What I does when I don't want to create a POJO for a weird response from the server is keep it as a JSON in java and parse the string to create a JSON object. (Yes because sometimes we just can't control what the API guy is coding...)

As Cristan said, it would be strange to create a 2016-06-10 class. So, better handle it directly as a JSON object for that particular case. You can access any attribute using a JSON container and even store it in a database that way.

What you need to do if you choose that path:

privateStringsendAlert(String lat, String lon) throws IOException, JSONException {

    Call<ResponseBody> call = cougarServices.postAlert(lat, lon);
    ResponseBody response = call.execute().body();
    JSONObject json = (response != null ? newJSONObject(response.string()) : null);
    returnhandleJsonRequest(json);

}

Post a Comment for "How Can I Parse An Associative Array Using Gson Converter In Retrofit?"