Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fetch JSON array without any key in Retrofit (Android)?

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?

like image 599
Kapil Rajput Avatar asked Feb 13 '17 12:02

Kapil Rajput


People also ask

Can JSON have value without key?

Keys must be strings, and values must be a valid JSON data type: string.

How do you post raw whole JSON in the body of a retrofit request?

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.

How do I get JSONArray from JSONArray?

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.


1 Answers

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();

like image 69
Mohamed Fadel Buffon Avatar answered Sep 20 '22 05:09

Mohamed Fadel Buffon