I'm quite new to development.
I know how to fetch from simple api's and store the data in kotlin data class something like this:
data class Property(
val id: String,
#Json(name = "img_src") val imgSrcUrl: String,
val type: String,
val price: Double
)
But how can i fetch and store data sturctue like this?
{
"Thailand": [
{
"date": "2020-1-22",
"confirmed": 2,
"deaths": 0,
"recovered": 0
},
{
"date": "2020-1-23",
"confirmed": 3,
"deaths": 0,
"recovered": 0
},
...
],
...
}
As #CommonsWare suggested in the comments, I was able to get the data like this:
fun getPropeties():
Call<Map<String, List<Property>>>
}
Related
I want to parse JSON like below:
{
"result": 0,
"list": [
{
"id": 58,
"type": "58",
"name": "fooGroup",
"foos": {
"id": "1",
"name": "33",
}
]
}
If I define models like this:
data class Response(val result: Int,
#SerializedName("list") val fooGroup: List<FooGroupResponse>)
data class FooGroupResponse(val id: Int, val type: String, val name: String,
#SerializedName("foos") val fooGroup: List<Foo>?)
data class Foo(val id: Int, val name: String)
then everything works fine.
Right now I want to take these out as a model:
"id": 58,
"type": "58",
"name": "fooGroup",
That is add one more model FooGroup like below:
data class Response(val result: Int,
#SerializedName("list") val fooGroup: List<FooGroupResponse>)
data class FooGroupResponse(val fooGroup: FoolGroup,
#SerializedName("foos") val fooGroup: List<Foo>?)
data class Foo(val id: Int, val name: String)
data class FooGroup(val id: Int, val type: String, val name: String)
But there's no #SerializedName can be set for FooGroup, is it possible?
Thanks.
I think you will have to restructure your JSON. The parser is going to make a faithful representation of the JSON string as a Java object. Your first example is the faithful representation. What you want to do is not faithful to the JSON received.
{
"result": 0,
"list": [
{
"fooGroup": {
"id": 58,
"type": "58",
"name": "fooGroup"
},
"foos": [
{
"id": "1",
"name": "33"
}
]
}
]
}
BUT you can do it manually where you parse the object yourself. Here is a how to with GSON but it should be easily convertible if you prefer another lib.
https://www.woolha.com/tutorials/retrofit-2-define-custom-gson-converter-factory
Simply pull "id", "type", "name" from the JSON as its deserializing and make a composite data class.
data class FooGroup(val id: Int, val type: String, val name: String)
//You deserialize you JSON into a List of Foos
data class Foos(val foo: Foo, val fooGroup : FooGroup)
How to generate request model having same object which having multiple types:
{
"questionnaire": 2,
"response": [
{
"answer": {
"id": 8,
"option_data": {
"description": "",
"text": "As much as i ever did",
"value": "4"
}
},
"question_id": 4
},
{
"answer": {
"option_data1": [
{
"text": "",
"value": 2
}
]
},
"question_id": 2
}
]
}
There is a plugin AS Json to DataClass convertor. You can find here
data class s(
val questionnaire: Int?, // 2
val response: List<Response>? )
data class Response(
val answer: Answer?,
#Json(name = "question_id")
val questionId: Int? // 4 )
data class OptionData1(
val text: String?,
val value: Int? // 2 )
data class OptionData(
val description: String?,
val text: String?, // As much as i ever did
val value: String? // 4 )
data class Answer(
val id: Int?, // 8
#Json(name = "option_data")
val optionData: OptionData?,
#Json(name = "option_data1")
val optionData1: List<OptionData1>? )
As you see there is ? in some fields. You can use it
from my local Django Rest Framework service I get the following JSON output:
{
"count": 5,
"next": null,
"previous": null,
"results": [
{
"id": 1,
"created": "2020-04-18T16:00:16.060915Z",
"name": "Germany",
"groups": [
{
"id": 1,
"created": "2020-04-18T16:03:11.138661Z",
"name": "MyGroup1",
"owner_id": 1
},
{
"id": 2,
"created": "2020-04-18T16:03:20.701660Z",
"name": "MyGroup2",
"owner_id": 1
},
...
Each Country can have many Groups. For this I have created the following data classes in my Android App project:
#JsonClass(generateAdapter = true)
data class NetworkCountryContainer(
val count: Long,
val next: String?,
val previous: String?,
val results: List<Country>
)
#Entity(tableName = "country_table")
#JsonClass(generateAdapter = true)
data class Country(
#PrimaryKey
#Json(name="id")
val countryId : Int,
#Json(name="name")
val countryName: String,
#Json(name="groups")
val groupList: List<Group> // <--- this field causes the ERROR
)
#Entity(tableName = "group_table")
#JsonClass(generateAdapter = true)
data class Group(
#PrimaryKey
#Json(name="id")
val groupId : Int,
#Json(name="name")
val groupName: String,
#Json(name="owner_id")
val ownerId: Int
)
Android Studio tells me this:
Cannot figure out how to save this field into database. You can consider adding a type converter for it.
Why I need a TypeConverter ? And how can I build one ?
how can I store a specific value of a key from json response into a variable
{
"results": [
{
"name": ryan,
"roll_id": 64,
"class_id": 310,
"net_id": 95,
},
],
};
above is the json response :-
val gson = GsonBuilder().create()
val ListV = gson.fromJson(body, HomeClass::class.java)
after these 2 lines I'm totally clueless how to do it I've gone through Internet but It was hard for me to understand how to proceed further.
Your Json Structure will be
{
"results": [
{
"name": "Amiyo",
"roll_id": 1,
"class_id": 10,
"net_id": 91
},
{
....
}
]
}
Data class should be
data class HomeClass (
#SerializedName("results") val results : List<Results>
)
data class Results (
#SerializedName("name") val name : String,
#SerializedName("roll_id") val roll_id : Int,
#SerializedName("class_id") val class_id : Int,
#SerializedName("net_id") val net_id : Int
)
fromJson
val listData = gson.fromJson(jsonData, HomeClass::class.java)
Then
val totalSize = 0 until listData!!.size
if(totalSize.size>0)
{
for (i in totalSize)
{
//Your Code i==Position
}
}
If I have a one-to-many Android Room relationship between two tables, expressed like this (using an example cut and paste from the Android developers site):
#Entity
data class User(
#PrimaryKey val userId: Long,
val name: String,
val age: Int
)
#Entity
data class Playlist(
#PrimaryKey val playlistId: Long,
val userCreatorId: Long,
val playlistName: String
)
data class UserWithPlaylists(
#Embedded val user: User,
#Relation(
parentColumn = "userId",
entityColumn = "userCreatorId"
)
val playlists: List<Playlist>
)
then when I use Retrofit2 to get the nested one-to-many object UserWithPlaylists from an API it's expecting JSON that looks more or less like this:
{
"user" : {
"userID": 1,
"name": "whatever",
"age": 10
},
"playlists": [
{
"playlistId": 0,
"playlistName": "whatever"
},
{
"playlistId": 1,
"playlistName": "whatever"
}
]
}
But my api (and as far as I can tell, most APIs) doesn't embed the user details in "user {}" like that. It just shows the user details unembedded, like this:
{
"userID": 1,
"name": "whatever",
"age": 10,
"playlists": [
{
"playlistId": 0,
"playlistName": "whatever"
},
{
"playlistId": 1,
"playlistName": "whatever"
}
]
}
So Retrofit2 would be able to create the playlists, but it won't find a user{} object so I would end up with empty user objects but populated playlist objects.
How would I force Retrofit2 look for the user details unembedded?
Apologies for my roundabout way of asking this question. I don't know the proper terminology for all of this, which has made it difficult for me to search.
thanks!
John