16

I have a JSON array without any object(key) inside which there are JSON Objects like this :

[
  {
    "Type": "Meeting",
    "Name": "TestMeeting",
    "StartDate": "2016-03-22T08:00:00",
    "EndDate": "2016-03-24T09:00:00"
  }
]

I tried to parse it but can't find success,Can anyone suggest me how to parse this type of Response using Retrofit?

3
  • 3
    List<Event>, where Event has fields for type, name, start date, and end date. Commented Feb 13, 2017 at 12:55
  • Why downvoted without any reason, i don't know what i have asked wrong Commented Feb 13, 2017 at 13:11
  • @CommonsWare thanks for replying your suggestion works :) Commented Feb 13, 2017 at 13:12

3 Answers 3

29

You Can define a Class representing the JSON Object

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Meeting{

@SerializedName("Type")
@Expose
private String type;
@SerializedName("Name")
@Expose
private String name;
@SerializedName("StartDate")
@Expose
private String startDate;
@SerializedName("EndDate")
@Expose
private String endDate;

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getStartDate() {
return startDate;
}

public void setStartDate(String startDate) {
this.startDate = startDate;
}

public String getEndDate() {
return endDate;
}

public void setEndDate(String endDate) {
this.endDate = endDate;
}

}

after that you define Callback for retrofit like that Call<List<Meeting>> getMeetings();

Sign up to request clarification or add additional context in comments.

2 Comments

10+ for direct requesting list Call<List<Meeting>> getMeetings();
what if its not an array?
1

if you are using Gson parsing then simple do it like this

 var model = Gson().fromJson(response.body()!!.string(), Array<Meeting>::class.java).toList()

Comments

0

Json Response :

{"contacts":{"918888302649":0,"917207251056":0,"918888804581":0}}

POJO Class :

public class ContactSyncResponseModel {

    @SerializedName("contacts")
    @Expose
    private Map<String, String> result;

    public Map<String, String> getResult() {
        return result;
    }

    public void setResult(Map<String, String> result) {
        this.result = result;
    } }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.