This is my first android project (I'm a php developer) but i'm struggling to see how to take this example which uses a nested structure under data for the json array whereas my data is not double nested like this example.
The excample code I am trying to modify is from here https://github.com/datanapps/RetrofitKotlin and the key differences in the json are todo with the nesting.
Original
{
"page": 1,
"per_page": 6,
"total": 12,
"total_pages": 2,
"data": [
{
"id": 1,
"email": "george.bluth#reqres.in",
"first_name": "George",
"last_name": "Bluth",
"avatar": "https://reqres.in/img/faces/1-image.jpg"
},
{
"id": 2,
"email": "james.smith#reqres.in",
"first_name": "James",
"last_name": "Smith",
"avatar": "https://reqres.in/img/faces/1-image.jpg"
}
]
}
whereas mine is simply
[
{
"_id": "62f4cb4e131d9d00046a1b9c",
"device": "AndroidAPS-DexcomG6",
"date": 1660209967000,
"dateString": "2022-08-11T09:26:07.000Z",
"isValid": true,
"sgv": 133,
"direction": "Flat",
"type": "sgv",
"created_at": "2022-08-11T09:26:38.326Z",
"mills": 1660209998326
},
{
"_id": "62f4cb4e131d9d00046a1b9c",
"device": "AndroidAPS-DexcomG6",
"date": 1660209967000,
"dateString": "2022-08-11T09:26:07.000Z",
"isValid": true,
"sgv": 133,
"direction": "Flat",
"type": "sgv",
"created_at": "2022-08-11T09:26:38.326Z",
"mills": 1660209998326
}
]
Specifically I want to query the SGV values, direction and date
Thanks
You "return type" of "method" in Retrofit "interface" will be:
List<MyModel>
And MyModel:
data class MyModel(val _id:String, val device:String,...)
Related
I am getting multiple fields dynamically from JSON data. To save data of dynamic fields into web services i prefer to call by id. But i unable to get the id of that fields. So I need to know how to get ids of dynamic fields.
Here is my JSON response:
{
"sections": [
{
"head": "Section A",
"sort": 1,
"section_id": 3,
"cols": "2",
"fields": [
{
"id": 5,
"template_id": 2,
"section_id": 3,
"label": "A1",
"itype": "1",
"idefault": "",
"ioptions": "",
"required": "N",
"ivalids": "0",
"sort": 1,
"created_at": "-0001-11-30 00:00:00",
"updated_at": "2018-09-23 05:32:59",
"deleted_at": null
},
{
"id": 6,
"template_id": 2,
"section_id": 3,
"label": "A2",
"itype": "1",
"idefault": "",
"ioptions": "",
"required": "N",
"ivalids": "0",
"sort": 1,
"created_at": "-0001-11-30 00:00:00",
"updated_at": "2018-09-23 05:32:59",
"deleted_at": null
}
]
}
]
}
If "itype"=1 dynamic_field = edittext, "itype"=2 dynamic_field =
spinner and so on.
In my response, I am getting "itype"=1 so i am dynamically displaying two edit texts.
And "label" from JSON used as id for a dynamic field. If I integrate API, it's getting the error as "can't resolve the id".
I don't know how to resolve this error.
Screenshot
Thanks in Advance. This is the response what i am getting. I need to insert this data to local database using room library and need to use auto synchronization in android.
I need to create a local database with this data . I am not clear how to save list and List in android.
{"email": "abc#gmail.com",
"createdOn": "1530344810442",
"childname": "kid1",
"statuscode": "Open",
"programs": ["a","b"],
"labels": [
"5b360152a987ba31b26ced9d",
"5b360152a987ba31b26ced98"
],
"enquiriedPrograms": [
"FIRST",
"Daycare"
],
"details": "Rosy [11:53 a.m. 10 Mar 2018 ] - Interested to join in next year",
"dateofenquiry": "1520682660000",
"followup": "1552132380000",
"channel": "Parent referral",
"academicYear": "2019-20",
"childDOB": "959668410000",
"country": "India",
"countryCode": "91",
"country_name_code": "IN",
"secodaryContact": "99563959718",
"secodaryCountry": "India",
"secodaryCountryCode": "91",
"secodarycountry_name_code": "IN",
"priority": "0",
"_id": "5b37356afa4f6c15a0474d08",
"parentname": "parent",
"mobile": "96542372812",
"schoolid": "59df41728832621654417350",
"__v": 0,
"labelNames": [
{
"_id": "5b360152a987ba31b26ced9d",
"name": "Add",
"colourCode": "#FFFF00"
},
{
"_id": "5b360152a987ba31b26ced98",
"name": "sub",
"colourCode": "#7FB3D5"
}
]
}
Here's the JSON where jsonObject key is dynamic :
{
"CH000032": [
{
"type": "event",
"details": {
"programID": "MV10000032",
"programType": "MOVIE",
"title": "Titanic",
"year": "1997",
"rating": "PG-13",
"durationSec": 11640,
"startTimeSec": "",
"endTimeSec": "",
"language": "ENG",
"isHD": true,
"Genres": [
"Movies",
"Action"
],
"description": "A seventeen-year-old aristocrat falls in love with a kind but poor artist aboard the luxurious, ill-fated R.M.S. Titanic.",
"imageUrl": "http://res.cloudinary.com/dte07foms/image/upload/c_scale,h_405,w_270/l_Copyright_e3jt1k/v1508831090/Titanic_b0hqo0.jpg"
}
}
],
"CH000033": [
{
"type": "event",
"details": {
"programID": "EP10000132",
"programType": "EPISODE",
"title": "A Chic Bar in Ibiza",
"seriesTitle": "Two and a Half Men",
"seasonNumber": 12,
"epsiodeNumber": 2,
"year": "2014",
"rating": "TV-14",
"durationSec": 1260,
"startTimeSec": "",
"endTimeSec": "",
"language": "ENG",
"isHD": true,
"Genres": [
"Comedy",
"Romance"
],
"description": "Alan has second thoughts about getting married when Walden has him sign a prenup.",
"imageUrl": "http://res.cloudinary.com/dte07foms/image/upload/c_crop,h_405,w_270//l_Copyright_e3jt1k/v1508831090/2AndHalfmen_splkro.jpg"
}
}
]
}
I would like to parse this JSON. Please let me know how should the class
be written with #SerializedName annotation using Retrofit.
Note : CH000032, CH000033 etc are dynamic.
You can use Map<String, ModelClassName> in your model class for dynamic like below :-
public class Data {
#SerializedName("your_key")
#Expose
private Map<String, ModelClassName> result;
//....
}
this can help to parse dynamic key in retrofit.
What you are asking will take much work , instead a better approach will be to change the structure o json to something like this -
"data": [
{
"type": "event",
"programID": "MV10000032",
"programType": "MOVIE",
"title": "Titanic",
"year": "1997",
"rating": "PG-13",
"durationSec": 11640,
"startTimeSec": "",
"endTimeSec": "",
"language": "ENG",
"isHD": true,
"Genres": [
"Movies",
"Action"
],
"description": "A seventeen-year-old aristocrat falls in love with a kind but poor artist aboard the luxurious, ill-fated R.M.S. Titanic.",
"imageUrl": "http://res.cloudinary.com/dte07foms/image/upload/c_scale,h_405,w_270/l_Copyright_e3jt1k/v1508831090/Titanic_b0hqo0.jpg"
},
{
"type": "event",
"programID": "MV10000032",
"programType": "MOVIE",
"title": "Titanic",
"year": "1997",
"rating": "PG-13",
"durationSec": 11640,
"startTimeSec": "",
"endTimeSec": "",
"language": "ENG",
"isHD": true,
"Genres": [
"Movies",
"Action"
],
"description": "A seventeen-year-old aristocrat falls in love with a kind but poor artist aboard the luxurious, ill-fated R.M.S. Titanic.",
"imageUrl": "http://res.cloudinary.com/dte07foms/image/upload/c_scale,h_405,w_270/l_Copyright_e3jt1k/v1508831090/Titanic_b0hqo0.jpg"
}]
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
I am parsing json object from facebook.
In my facebook json object, there are string keys inside key array "data".
My Code as follows,
"data": [
{
"id": "100001211447563_300696056647440",
"from": {
"name": "Seho Lee",
"id": "100001211447563"
},
"story": "Seho Lee is now using Facebook in English (US).",
"story_tags": {
"0": [
{
"id": 100001211447563,
"name": "Seho Lee",
"offset": 0,
"length": 8
}
]
},
"type": "status",
"created_time": "2012-01-19T09:13:04+0000",
"updated_time": "2012-01-19T09:13:04+0000",
"comments": {
"count": 0
}
},
{
"id": "100001211447563_298802933503419",
"from": {
"name": "Seho Lee",
"id": "100001211447563"
},
"story": "Seho Lee started using Graph API Explorer.",
"story_tags": {
"23": [
{
"id": 145634995501895,
"name": "Graph API Explorer",
"offset": 23,
"length": 18
}
],
"0": [
{
"id": 100001211447563,
"name": "Seho Lee",
"offset": 0,
"length": 8
}
]
},
"picture": "http://photos-a.ak.fbcdn.net/photos-ak-snc1/v43/11/145634995501895/app_1_145634995501895_4870.gif",
"link": "http://developers.facebook.com/tools/explorer/",
"caption": "A tool to help you browse objects within the Facebook Graph API, manage permissions, obtain access tokens and generally learn how it all works.",
"actions": [
{
"name": "Comment",
"link": "http://www.facebook.com/100001211447563/posts/298802933503419"
},
{
"name": "Like",
"link": "http://www.facebook.com/100001211447563/posts/298802933503419"
}
],
"type": "link",
"created_time": "2012-01-16T09:47:57+0000",
"updated_time": "2012-01-16T09:47:57+0000",
"comments": {
"count": 0
}
},
{
"id": "100001211447563_298789650171414",
"from": {
"name": "Seho Lee",
"id": "100001211447563"
},
"story": "Seho Lee likes myFBCovers.com.",
"story_tags": {
"15": [
{
"id": 160901873998019,
"name": "myFBCovers.com",
"offset": 15,
"length": 14
}
],
"0": [
{
"id": 100001211447563,
"name": "Seho Lee",
"offset": 0,
"length": 8
}
]
},
"type": "status",
"created_time": "2012-01-16T08:57:33+0000",
"updated_time": "2012-01-16T08:57:33+0000",
"comments": {
"count": 0
}
}
when I parsing this json object, String key "likes" is sometimes exist and sometimes do not exist in "data" jsonArray. So when I parsing JsonString "likes", sometimes I gets JSONException since I am parsing null value. Is there any way to skipping "null" json value?
Please help me regarding this.
if try catch it, next processes will be skipped.
you can check by isNull("name") method.
Edit: this is to check key. since you mentioned key exists sometimes. did you mean the value?
use JSONObjects method has(key)
data = (JSONObject) new JSONTokener(jsonObjRecv.toString()).nextValue();
if data.has("like"){
...
}
to check whether the value is null check with isNull("name")