I'm making a project (for a programming class in uni) but when I try to run it in Android Studio, a warning appears very briefly in the emulator:
java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 5 column 21 path $[0].dateOfBirth
This is my JSON file:
{
"fighters": [
{
"id": 1,
"name": "Karl",
"dateOfBirth": "20-03-1975",
"level": 4,
"image": "karl.png"
},
{
"id": 2,
"name": "Geralt",
"dateOfBirth": "16-08-1964",
"level": 8,
"image": "Geralt.png"
},
{
"id": 3,
"name": "Darrak",
"dateOfBirth": "25-11-1940",
"level": 5,
"image": "Darrak.png"
},
{
"id": 4,
"name": "Jafar",
"dateOfBirth": "09-02-1920",
"level": 9,
"image": "Jafar.png"
},
{
"id": 5,
"name": "Cornelius",
"dateOfBirth": "28-06-1988",
"level": 2,
"image": "Cornelius.png"
},
{
"id": 6,
"name": "Laila",
"dateOfBirth": "18-10-1998",
"level": 5,
"image": "Laila.png"
},
{
"id": 7,
"name": "Marianne",
"dateOfBirth": "01-03-1975",
"level": 7,
"image": "Marianne.png"
},
{
"id": 8,
"name": "Petro",
"dateOfBirth": "10-07-1974",
"level": 10,
"image": "Petro.png"
},
{
"id": 9,
"name": "Ordelia",
"dateOfBirth": "18-05-1985",
"level": 5,
"image": "Ordelia.png"
},
{
"id": 10,
"name": "Lucina",
"dateOfBirth": "21-09-1992",
"level": 9,
"image": "Lucina.png"
},
{
"id": 11,
"name": "Hugo",
"dateOfBirth": "16-07-1938",
"level": 6,
"image": "Hugo.png"
},
{
"id": 12,
"name": "Sildar",
"dateOfBirth": "19-12-1980",
"level": 3,
"image": "Sildar.png"
},
{
"id": 13,
"name": "Zenok",
"dateOfBirth": "30-10-1999",
"level": 1,
"image": "Zenok.png"
},
{
"id": 14,
"name": "Violet",
"dateOfBirth": "02-04-2001",
"level": 8,
"image": "Violet.png"
},
{
"id": 15,
"name": "Tamara",
"dateOfBirth": "13-06-1963",
"level": 4,
"image": "Tamara.png"
}
],
"encounters": [
{
"id": 1,
"fighterId": 1,
"amount_of_monsters": 4,
"difficulty": "Medium"
},
{
"id": 2,
"fighterId": 5,
"amount_of_monsters": 1,
"difficulty": "Easy"
},
{
"id": 3,
"fighterId": 5,
"amount_of_monsters": 2,
"difficulty": "Medium"
},
{
"id": 4,
"fighterId": 7,
"amount_of_monsters": 1,
"difficulty": "Hard"
},
{
"id": 5,
"fighterId": 11,
"amount_of_monsters": 7,
"difficulty": "Medium"
},
{
"id": 6,
"fighterId": 7,
"amount_of_monsters": 2,
"difficulty": "Easy"
},
{
"id": 7,
"fighterId": 14,
"amount_of_monsters": 10,
"difficulty": "Extreme"
},
{
"id": 8,
"fighterId": 13,
"amount_of_monsters": 4,
"difficulty": "Medium"
},
{
"id": 9,
"fighterId": 7,
"amount_of_monsters": 5,
"difficulty": "Hard"
},
{
"id": 10,
"fighterId": 3,
"amount_of_monsters": 5,
"difficulty": "Easy"
}
]
}
Line 5 is "name": "Karl",
There's something wrong with my dateOfBirth attribute and I don't know why, because to me the syntax looks correct. I've tried reinstalling the app and rebuilding the project but that didn't work.
This is my first time posting a question on StackOverflow, so apologies if something isn't clear.
If anyone is able to help, I would greatly appreciate it.
EDIT (additional info)
I'm using Android Studio 3.6, API 29.
Android Gradle Plugin Version: 3.5.3
Gradle Version: 5.4.1
I use GSON to parse the JSON
JDK 11
This is the function I used for the GSON class:
fun getFighters(): Observable<Array<Fighter>> {
val observable = Observable.create<Array<Fighter>> { emitter ->
try {
var connection = connect("${BASE_URL}/fighters")
val gson = GsonBuilder().create()
val fighters = gson.fromJson(
InputStreamReader(connection.inputStream),
Array<Fighter>::class.java
)
for (fighter in fighters) {
connection = connect("${BASE_URL}/${fighter.image}")
fighter.imageBitmap = BitmapFactory.decodeStream(connection.inputStream)
}
emitter.onNext(fighters)
} catch(e: Exception) {
emitter.onError(e)
}
}
return observable
}
The BASE_URL is just a testing url since the assignment we have to do doesn't require to actually deploy the app. The for loop is to show the images of the Fighters in a RecyclerView list, so therefore I used a bitmap. Furthermore, here's the basic Fighter data class:
data class Fighter(
val id: Number,
val name: String,
val dateOfBirth: LocalDate,
val level: Number,
val image: String,
var imageBitmap: Bitmap
)
I think the class that you are using for parsing the JSON should be modified as follows.
data class Fighter(
val id: Number,
val name: String,
val dateOfBirth: String,
val level: Number,
val image: String,
var imageBitmap: Bitmap
)
The dateOfBirth is stored as a String in your JSON and you need to fetch that in that way. If you need to convert the value to a LocalDate object, you can always do that later after parsing the information. I hope that helps!
Related
I want to get only those item which contain type Lunch `` This is a function where I can fetch data from Rest Api and also ApiResponse added
I need to get specific data from a session I send instead of getting the entire array from the response from the Api() method. How do I capture just the only those item which contain type Lunch from the array?
{
"success": true,
"data": [
{
"id": 6,
"name": "Snack",
"type": "breakfast",
"detail": "18",
"image": "uploads/Screenshot_2022-12-04-13-15-13-27.jpg",
"carbs": 6,
"proteins": 4,
"fats": 8,
"cal": 4,
"day": 1,
"created_at": null,
"updated_at": null
},
{
"id": 7,
"name": "Chicken",
"type": "lunch",
"detail": "White Chicken",
"image": "uploads/1670258808164716637219.jpg",
"carbs": 15,
"proteins": 18,
"fats": 10,
"cal": 200,
"day": 1,
"created_at": null,
"updated_at": null
},
{
"id": 8,
"name": "546e57rufjvjv",
"type": "lunch",
"detail": "gdyhj",
"image": "uploads/meat.png",
"carbs": 6578,
"proteins": 2345,
"fats": 5678,
"cal": 4325,
"day": 5,
"created_at": null,
"updated_at": null
},
{
"id": 9,
"name": "Anda Tikki",
"type": "dinner",
"detail": "Egg and Daal",
"image": "uploads/1670259245043112842388.jpg",
"carbs": 10,
"proteins": 8,
"fats": 12,
"cal": 180,
"day": 1,
"created_at": null,
"updated_at": null
},
{
"id": 10,
"name": "Boiled egg and cucumber",
"type": "breakfast",
"detail": "Egg and cucumber",
"image": "uploads/1670259362662817976982.jpg",
"carbs": 10,
"proteins": 5,
"fats": 5,
"cal": 100,
"day": 2,
"created_at": null,
"updated_at": null
}
],
"message": "Food Data retrieved successfully."
}
Future<List<ProductsModel>> getProducts({String? query}) async {
List<ProductsModel> products = [];
var data=[];
try {
String url = 'https://diet.appetitor.app/Celo/api/user/food/2000.0';
var response = await Dio().get(url,
options: Options(headers: {
HttpHeaders.contentTypeHeader: "application/json",
}));
if (response.statusCode == 200) {
products.clear();
ProductsModel.fromJson(response.data);
products.add(ProductsModel.fromJson(response.data));
if (query != null) {
print(products.length);
products = products
.map((e) {
e.data!.where((element) => element.type!.toLowerCase().contains('lunch'));
})
.cast<ProductsModel>()
.toList();
}
}
} on DioError catch (e) {
print(e.response);
}
return products;
}
}
If the response status code returns 200 after making a request with the rest api, assign the data in it to a variable of the type of your model, then you can get it from the variable as you want.
I am working on gallery module in which the URL of images coming from web API.In the API there is an event name,image url and description of event.now i want an Album view of particular event.i want to make album of particular event with by default first image and clicking on that album display another images of event.
I want view like this which displayed in image.there is an photo album of particular event and by clicking on that photo album another images of that events are shown.
image
This is My Json Response.
[
{
"Rownumber": 1,
"id": 251,
"image_url": "~/gallery/1635242229_1998256693552243_3617232473282314240_n.jpg",
"Name": "DAMPATI SHIBIR",
"Description": "VIVAHIK JIVAN",
"CreatedDate": "2018-07-02T01:17:23.773",
"CreatedBy": "HIMANSHU_ADMIN",
"TotalRecords": 10
},
{
"Rownumber": 2,
"id": 252,
"image_url": "~/gallery/1635272131_1998256603552252_3478881719728209920_n.jpg",
"Name": "DAMPATI SHIBIR",
"Description": "VIVAHIK JIVAN",
"CreatedDate": "2018-07-02T01:17:23.773",
"CreatedBy": "HIMANSHU_ADMIN",
"TotalRecords": 10
},
{
"Rownumber": 3,
"id": 253,
"image_url": "~/gallery/16WhatsAppImage2018-06-13at11.21.20AM.jpeg",
"Name": "DAMPATI SHIBIR",
"Description": "VIVAHIK JIVAN",
"CreatedDate": "2018-07-02T01:17:23.79",
"CreatedBy": "HIMANSHU_ADMIN",
"TotalRecords": 10
},
{
"Rownumber": 4,
"id": 247,
"image_url": "~/gallery/",
"Name": "MAHAVIR",
"Description": "",
"CreatedDate": "2018-01-27T15:19:49.553",
"CreatedBy": "Bhavesh",
"TotalRecords": 10
},
{
"Rownumber": 5,
"id": 248,
"image_url": "~/gallery/1510991329_460478337432578_9007253566282945229_n.jpg",
"Name": "MAHAVIR",
"Description": "",
"CreatedDate": "2018-01-27T15:20:03.367",
"CreatedBy": "Bhavesh",
"TotalRecords": 10
},
{
"Rownumber": 6,
"id": 249,
"image_url": "~/gallery/15290643_118745158223390_7028747_o.jpg",
"Name": "MAHAVIR",
"Description": "",
"CreatedDate": "2018-01-27T15:20:26.183",
"CreatedBy": "Bhavesh",
"TotalRecords": 10
},
{
"Rownumber": 7,
"id": 250,
"image_url": "~/gallery/14304977_314992211932016_2142854253_n.jpg",
"Name": "MAHAVIR SANKARDHAM 2016",
"Description": "",
"CreatedDate": "2018-01-27T02:57:12.157",
"CreatedBy": "Bhavesh",
"TotalRecords": 10
},
{
"Rownumber": 8,
"id": 244,
"image_url": "~/gallery/14295597_118745108223395_4830375_n.jpg",
"Name": "MAHAVIR SANKARDHAM 2016",
"Description": "",
"CreatedDate": "2018-01-27T00:00:00",
"CreatedBy": "Bhavesh",
"TotalRecords": 10
},
{
"Rownumber": 9,
"id": 245,
"image_url": "~/gallery/1410372160_410043112476101_1188375874765955878_n.jpg",
"Name": "MAHAVIR SANKARDHAM 2016",
"Description": "",
"CreatedDate": "2018-01-27T15:14:12.17",
"CreatedBy": "Bhavesh",
"TotalRecords": 10
},
{
"Rownumber": 10,
"id": 246,
"image_url": "~/gallery/14304977_314992211932016_2142854253_n.jpg",
"Name": "MAHAVIR SANKARDHAM 2016",
"Description": "",
"CreatedDate": "2018-01-27T15:14:42.58",
"CreatedBy": "Bhavesh",
"TotalRecords": 10
}
]
i want to display images in a particualr album which is having same name.
Steps :-
Parse the JSON response to a model
Bind the model to recyclerview
Add GLide library to handle images from Image URL.
Attached a tutorial:-
Gallery
First Creat a Data Class:
data class AlbumInfo(var Rownumber:Int,
var id:Int,
var image_url :String,
var Name :String,
var Description :String,
var CreatedDate :String,
var TotalRecords :String
)
Then the other Data Class for Album
data class AlbumName(var albumName:String, var url :String,var list:List<AlbumInfo>)
Then just do the groupBy() and you will get the required structure
I did on the Basis of Name
var result= list.groupBy { it.Name}.entries.map { (name, group) ->
AlbumName(name, group[0].image_url,group)
}
I guess that will help you. Use the result for your recyclerview.
You will get a response like this.
[
AlbumName(albumName=DAMPATISHIBIR,
url=~/gallery/1635242229_1998256693552243_3617232473282314240_n.jpg,
list=[
AlbumInfo(Rownumber=1,
id=1,
image_url=~/gallery/1635242229_1998256693552243_3617232473282314240_n.jpg,
Name=DAMPATISHIBIR,
Description=VIVAHIKJIVAN,
CreatedDate=2018-07-02T01: 17: 23.773,
TotalRecords=20),
AlbumInfo(Rownumber=2,
id=252,
image_url=~/gallery/1635272131_1998256603552252_3478881719728209920_n.jpg,
Name=DAMPATISHIBIR,
Description=VIVAHIKJIVAN,
CreatedDate=2018-07-02T01: 17: 23.773,
TotalRecords=22)
]),
AlbumName(albumName=MAHAVIR,
url=~/gallery/1635272131_1998256603552252_3478881719728209920_n.jpg,
list=[
AlbumInfo(Rownumber=3,
id=252,
image_url=~/gallery/1635272131_1998256603552252_3478881719728209920_n.jpg,
Name=MAHAVIR,
Description=VIVAHIKJIVAN,
CreatedDate=2018-07-02T01: 17: 23.773,
TotalRecords=22),
AlbumInfo(Rownumber=4,
id=252,
image_url=~/gallery/1635272131_1998256603552252_3478881719728209920_n.jpg,
Name=MAHAVIR,
Description=VIVAHIKJIVAN,
CreatedDate=2018-07-02T01: 17: 23.773,
TotalRecords=22)
]),
AlbumName(albumName=MAHAVIRSANKARDHAM2016,
url=~/gallery/1635272131_1998256603552252_3478881719728209920_n.jpg,
list=[
AlbumInfo(Rownumber=2,
id=252,
image_url=~/gallery/1635272131_1998256603552252_3478881719728209920_n.jpg,
Name=MAHAVIRSANKARDHAM2016,
Description=VIVAHIKJIVAN,
CreatedDate=2018-07-02T01: 17: 23.773,
TotalRecords=22)
])
]
I'm new to oop and android dev and am trying to extract only a small part of this json file.I want to get back only the "heavyweight" rankings, no other weight division.. Any help would be highly appreciated!
Here is my model:
data class Rankings(
#SerializedName("rankings")
val rankings: List<Ranking>
)
data class Ranking(
#SerializedName("competitor_rankings")
val competitorRankings: List<CompetitorRanking>,
#SerializedName("name")
val name: String,
#SerializedName("type_id")
val typeId: Int,
#SerializedName("week")
val week: Int,
#SerializedName("year")
val year: Int
)
data class CompetitorRanking(
#SerializedName("competitor")
val competitor: Competitor,
#SerializedName("rank")
val rank: Int
)
data class Competitor(
#SerializedName("abbreviation")
val abbreviation: String,
#SerializedName("id")
val id: String,
#SerializedName("name")
val name: String
)
here is my api interface:
#GET("ufc/trial/v2/en/rankings.json")
suspend fun getRankings(#Query("api_key") api_key: String): Rankings
companion object {
fun create(): ApiInterface {
val retrofit = Retrofit.Builder()
.baseUrl("https://api.sportradar.us/")
.addCallAdapterFactory(CoroutineCallAdapterFactory.invoke())
.addConverterFactory(GsonConverterFactory.create())
.build()
return retrofit.create(ApiInterface::class.java)
}
}
}
here's my api call:
lifecycleScope.launch(Dispatchers.IO) {
val result = api.getRankings("MY_API_KEY_WILL_GO_HERE")
Log.d(TAG,"${result.rankings}")
}
and here's my json file(I couldn't post the entire thing because of character limit, but it's essentially just 8 more rankings arrays):
"rankings": [
{
"type_id": 8,
"name": "pound_for_pound",
"year": 2020,
"week": 21,
"competitor_rankings": [
{
"rank": 1,
"movement": 0,
"competitor": {
"id": "sr:competitor:253371",
"name": "Jones, Jon",
"abbreviation": "JON"
}
},
{
"rank": 2,
"movement": 0,
"competitor": {
"id": "sr:competitor:250879",
"name": "Nurmagomedov, Khabib",
"abbreviation": "NUR"
}
},
{
"rank": 3,
"movement": 1,
"competitor": {
"id": "sr:competitor:237684",
"name": "Miocic, Stipe",
"abbreviation": "MIO"
}
},
{
"rank": 4,
"movement": 1,
"competitor": {
"id": "sr:competitor:410485",
"name": "Adesanya, Israel",
"abbreviation": "ADE"
}
},
{
"rank": 5,
"movement": 1,
"competitor": {
"id": "sr:competitor:253373",
"name": "Cormier, Daniel",
"abbreviation": "COR"
}
},
{
"rank": 6,
"movement": 1,
"competitor": {
"id": "sr:competitor:273539",
"name": "Usman, Kamaru",
"abbreviation": "USM"
}
},
{
"rank": 7,
"movement": 1,
"competitor": {
"id": "sr:competitor:290262",
"name": "Volkanovski, Alex",
"abbreviation": "VOL"
}
},
{
"rank": 8,
"movement": 1,
"competitor": {
"id": "sr:competitor:237676",
"name": "McGregor, Conor",
"abbreviation": "MCG"
}
},
{
"rank": 9,
"movement": 1,
"competitor": {
"id": "sr:competitor:237652",
"name": "Holloway, Max",
"abbreviation": "HOL"
}
},
{
"rank": 10,
"movement": 1,
"competitor": {
"id": "sr:competitor:351762",
"name": "Gaethje, Justin",
"abbreviation": "GAE"
}
},
{
"rank": 11,
"movement": 1,
"competitor": {
"id": "sr:competitor:261799",
"name": "Poirier, Dustin",
"abbreviation": "POI"
}
},
{
"rank": 12,
"movement": 1,
"competitor": {
"id": "sr:competitor:237646",
"name": "Ferguson, Tony",
"abbreviation": "FER"
}
},
{
"rank": 13,
"movement": 1,
"competitor": {
"id": "sr:competitor:253377",
"name": "Whittaker, Robert",
"abbreviation": "WHI"
}
},
{
"rank": 14,
"movement": 1,
"competitor": {
"id": "sr:competitor:274653",
"name": "Woodley, Tyron",
"abbreviation": "WOO"
}
},
{
"rank": 15,
"movement": 0,
"competitor": {
"id": "sr:competitor:250145",
"name": "Ngannou, Francis",
"abbreviation": "NGA"
}
}
]
},
{
"type_id": 16,
"name": "heavyweight",
"year": 2020,
"week": 21,
"competitor_rankings": [
{
"rank": 0,
"movement": 0,
"competitor": {
"id": "sr:competitor:237684",
"name": "Miocic, Stipe",
"abbreviation": "MIO"
}
},
{
"rank": 1,
"movement": 0,
"competitor": {
"id": "sr:competitor:253373",
"name": "Cormier, Daniel",
"abbreviation": "COR"
}
},
{
"rank": 2,
"movement": 0,
"competitor": {
"id": "sr:competitor:250145",
"name": "Ngannou, Francis",
"abbreviation": "NGA"
}
},
{
"rank": 3,
"movement": 0,
"competitor": {
"id": "sr:competitor:542009",
"name": "Blaydes, Curtis",
"abbreviation": "BLA"
}
},
{
"rank": 4,
"movement": 0,
"competitor": {
"id": "sr:competitor:237636",
"name": "Dos Santos, Junior",
"abbreviation": "DOS"
}
},
{
"rank": 4,
"movement": 1,
"competitor": {
"id": "sr:competitor:542099",
"name": "Lewis, Derrick",
"abbreviation": "LEW"
}
},
{
"rank": 6,
"movement": 0,
"competitor": {
"id": "sr:competitor:542143",
"name": "Rozenstruik, Jairzinho",
"abbreviation": "ROZ"
}
},
{
"rank": 7,
"movement": 0,
"competitor": {
"id": "sr:competitor:542161",
"name": "Volkov, Alexander",
"abbreviation": "VOL"
}
},
{
"rank": 8,
"movement": 0,
"competitor": {
"id": "sr:competitor:237694",
"name": "Overeem, Alistair",
"abbreviation": "OVE"
}
},
{
"rank": 9,
"movement": 0,
"competitor": {
"id": "sr:competitor:254231",
"name": "Harris, Walt",
"abbreviation": "HAR"
}
},
{
"rank": 10,
"movement": 0,
"competitor": {
"id": "sr:competitor:542117",
"name": "Oleinik, Aleksei",
"abbreviation": "OLE"
}
},
{
"rank": 11,
"movement": 0,
"competitor": {
"id": "sr:competitor:244080",
"name": "Abdurakhimov, Shamil",
"abbreviation": "ABD"
}
},
{
"rank": 12,
"movement": 0,
"competitor": {
"id": "sr:competitor:542079",
"name": "Ivanov, Blagoy",
"abbreviation": "IVA"
}
},
{
"rank": 13,
"movement": 0,
"competitor": {
"id": "sr:competitor:542145",
"name": "Sakai, Augusto",
"abbreviation": "SAK"
}
},
{
"rank": 14,
"movement": 0,
"competitor": {
"id": "sr:competitor:515358",
"name": "Pavlovich, Sergey",
"abbreviation": "PAV"
}
},
{
"rank": 15,
"movement": 0,
"competitor": {
"id": "sr:competitor:257349",
"name": "Werdum, Fabricio",
"abbreviation": "WER"
}
}
]
},
You can just use List.first method:
lifecycleScope.launch(Dispatchers.IO) {
val result = api.getRankings("MY_API_KEY_WILL_GO_HERE")
val ranking = result.rankings.first { it.name == "heavyweight" }
}
rankings.filter { it.name == "heavyweight" } will give you a list of heavyweight rankings. But it would be much better if your API had a way to filter those results on server side i.e. adding some filtering parameter to your request rather than getting all the results and filtering them on client side.
I get a JSON response like this from a server:
{
"name_1": [
{
"id": 1,
"type": 1,
"value": "25"
},
{
"id": 2,
"type": 2,
"value": "25"
}
],
"name_2": [
{
"id": 3,
"type": 1,
"value": "25"
},
{
"id": 6,
"type": 4,
"value": "25"
},
{
"id": 7,
"type": 4,
"value": "25"
}
],
"name_3": [
{
"id": 8,
"type": 1,
"value": "25"
}
]
}
How can I generate a POJO file for this in order to use it for auto-deserializing with retrofit?
I tried www.jsonschema2pojo.org, but it wants to create a new class for each JSON object, which is a list (name_1, name_2, name_3), but the count of "name_X" arrays could be different, as well as ther names.
So how can I create a POJO java class for auto-deserializing with GsonConverterFactory in retrofit?
Thanks! :)
You can use Map<String,List<YourObject>> where YourObject contains the id, type and value.
So, response should be :
public class YourObject {
public Map<String, NameDetail> nameDetail;
}
public class NameDetail {
public int id;
public int type;
public String value;
}
And your service with retrofit should return :
Call<YourObject> getStuff();
Your JSON structure is wrong. You should have something like this in order to get list of names:
{
"names" : [
{
"name": "name_1",
"values": [
{
"id": 1,
"type": 1,
"value": "25"
},
{
"id": 2,
"type": 2,
"value": "25"
}
]
},
{
"name": "name_2",
"values": [
{
"id": 3,
"type": 1,
"value": "25"
},
{
"id": 6,
"type": 4,
"value": "25"
},
{
"id": 7,
"type": 4,
"value": "25"
}
]
},
{
"name": "name_3",
"values": [
{
"id": 8,
"type": 1,
"value": "25"
}
]
}
]}
Or if you can't affect server-side then use HashMap<String,List<T>> like Skizo-ozᴉʞS said but it would be the best to change the structure.
I have a json response that looks somewhat like this.
{
"id": 1,
"username": "user",
"email": "generic user#gmail.com",
"name": "harshvardhan",
"phone_no": 2147483647,
"created_at": "2016-06-27 12:23:25",
"updated_at": "2016-06-27 12:23:25",
"hash": "",
"groups": [
{
"id": 37,
"title": "aspernatur-et-rem-quos-eius-voluptatem-eveniet-aut",
"descr": "33",
"identifier": "5771545383a2d",
"no_of_members": 0,
"created_at": "2016-06-27 21:59:07",
"pivot": {
"user_id": 1,
"grp_id": 37,
"roles": "admin"
}
},
{
"id": 67,
"title": "quibusdam-voluptas-non-facere-nihil",
"descr": "194",
"identifier": "57715453b8d80",
"no_of_members": 0,
"created_at": "2016-06-27 21:59:07",
"pivot": {
"user_id": 1,
"grp_id": 67,
"roles": "admin"
}
},
{
"id": 161,
"title": "libero-id-ipsa-beatae-aut",
"descr": "59",
"identifier": "5771545469db4",
"no_of_members": 0,
"created_at": "2016-06-27 21:59:08",
"pivot": {
"user_id": 1,
"grp_id": 161,
"roles": "admin"
}
},
{
"id": 198,
"title": "iure-ad-sunt-id-delectus-laboriosam-quo",
"descr": "150",
"identifier": "57715454a9d15",
"no_of_members": 0,
"created_at": "2016-06-27 21:59:08",
"pivot": {
"user_id": 1,
"grp_id": 198,
"roles": "admin"
}
}
]
}
There is a many to many relationship between users and groups.
I am using retrofit with the GsonConverterFactory. On response, how can I parse this information into objects?
I tried making a User class , and a group class, then I put an array of groups as a member of the user class.
But when I access User.getGroups().get(1).getID , it returns 0.
However, User.getGroups().size() does return 4 (the correct number in this case)
How can I achieve this in retrofit?
A practical tool to parse JSON correctly with retrofit can be to generate the classes with http://www.jsonschema2pojo.org
You just have to put the example JSON, the resource type in JSON and put the desired annotation style (GSON, Jackson, ..).
Finally click on the preview button and you can see the results