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
?
Keys must be strings, and values must be a valid JSON data type: string.
The Best Answer is The @Body annotation defines a single request body. Since Retrofit uses Gson by default, the FooRequest instances will be serialized as JSON as the sole body of the request. The Gson docs have much more on how object serialization works. And then use an instance of that class similar to #1.
JSONArray jsonArray = (JSONArray) jsonObject. get("contact"); The iterator() method of the JSONArray class returns an Iterator object using which you can iterate the contents of the current array.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With