https://api.mojilala.com/v1/stickers/trending?api_key=dc6zaTOxFJmzC
data class RootApi(val data : List<AllData>)
data class AllData(val images : ImageSticker)
data class ImageSticker(val fixed_height : ActualImage)
data class ActualImage(val url : String)
is this the correct POJO class or not according to given api
Firstly, hide your API key in the question. Coming to generating data classes of JSON. I would suggest you use the following Plugin
https://plugins.jetbrains.com/plugin/9960-json-to-kotlin-class-jsontokotlinclass-
And no that is not the correct data class. Infact it is very big. I can't even paste it all in the answer. Use the plugin. Paste the JSON response and generate yourself.
Following the answer from #che10 you should end up with the below:
data class Data(
// Assuming you are using Moshi https://github.com/square/moshi
// Use #SerializedName("data") if you're using Gson
#Json(name="data")
val stickers: List<Sticker>,
val meta: Meta,
val pagination: Pagination
)
data class Sticker(
val designer: String,
val id: String,
val images: Images,
val is_animated: Boolean,
val rating: String,
val tags: List<String>,
val url: String
)
data class Pagination(
val offset: String,
val total: Int,
val total_count: Int
)
data class Meta(
val msg: String,
val status: Int
)
// Different fields but of two types so we can reuse the classes
// Difference between AnimatedImage and StillImage is the two webp fields
data class Images(
// Animated images
val fixed_height: AnimatedImage,
val fixed_height_downsampled: AnimatedImage,
val fixed_height_medium: AnimatedImage,
val fixed_height_medium_downsampled: AnimatedImage,
val fixed_height_small: AnimatedImage,
val fixed_width: AnimatedImage,
val fixed_width_downsampled: AnimatedImage,
val fixed_width_medium: AnimatedImage,
val fixed_width_medium_downsampled: AnimatedImage,
val fixed_width_small: AnimatedImage,
// Still images
val fixed_height_still: StillImage,
val fixed_height_medium_still: StillImage,
val fixed_height_small_still: StillImage,
val fixed_width_still: StillImage,
val fixed_width_medium_still: StillImage,
val fixed_width_small_still: StillImage,
)
data class StillImage(
val url: String,
val size: Int,
val height: Int,
val width: Int,
)
data class AnimatedImage(
val url: String,
val size: Int,
val height: Int,
val width: Int,
val webp: String,
val webp_size: Int
)
Related
need help in parsing this json file from this endpoint
http://ergast.com/api/f1/constructors
this is my data class
data class Teams(
val MRData: MRdata
){
data class MRdata(
val ConstructorTable: ConstructorsTable,
val limit: String,
val offset: String,
val series: String,
val total: String,
val url: String,
val xmlns: String
){
data class ConstructorsTable(
val Constructors: List<Constructor>?
) {
data class Constructor(
val constructorId: String?,
val name: String?,
val nationality: String?,
val url: String?
)
}
}
}
when i use Teams class as a return model it logs actual data but, when i try to return specific data like constructorID or nationality it returns null.
Im trying to implement diffutil in my adapter and im getting the following error:
One type argument expected for class Result<out T>
This is the code where i get the error:
private val diffCallback = object: DiffUtil.ItemCallback<Result>() {
}
This is my Result class:
data class Result(
val adult: Boolean,
val backdrop_path: String,
val genre_ids: List<Int>,
val id: Int,
val media_type: String,
val original_language: String,
val original_title: String,
val overview: String,
val popularity: Double,
val poster_path: String,
val release_date: String,
val title: String,
val video: Boolean,
val vote_average: Double,
val vote_count: Int
)
I don't understand,this is exactly how i do this every time,and it works without problems
Kotlin already has a Result<T> type, and it's imported by default. You might need to fully qualify your own class name if you want to make sure you're using that one. Or maybe just rename your class, it will make things easier.
I have the next json as response:
{"id":1581,"title":"hhhh","tags":[],"iconUrl":"images/b94cdcde-9c6c-4ea1-9c22-47b4c1750cce","valoration":0,"postList":[{"id":41,"x":0.58,"y":0.3,"rotation":338,"resourceUrl":"images/ba4202c2-845f-4b70-8167-5ef4ffb347bf","valoration":0}]}
And I have declared the response at my project as:
#Serializable
data class BoardResponse(
#SerialName("id")
val id: Int,
#SerialName("title")
val title: String,
#SerialName("tags")
val tags: List<String>,
#SerialName("iconUrl")
val iconUrl: String,
#SerialName("valoration")
val valoration: Float,
#SerialName("postList")
val posts: List<PostResponse>
)
The problem is that I'm getting allways a null in the "posts" value.
This is the PostResponse:
#Serializable
data class PostResponse(
#SerialName("id")
val id: Int,
#SerialName("x")
val x: Float,
#SerialName("y")
val y: Float,
#SerialName("rotation")
val rotation: Int,
#SerialName("resourceUrl")
val resourceUrl: String,
#SerialName("valoration")
val valoration: Float
)
Can I do what I'm trying to do in some way?
Your code is correct. With the exact same declarations as yours, and with the example JSON you provided, the following test passes (you get non-null posts):
#Test
fun test() {
val json = """{"id":1581,"title":"hhhh","tags":[],"iconUrl":"images/b94cdcde-9c6c-4ea1-9c22-47b4c1750cce","valoration":0,"postList":[{"id":41,"x":0.58,"y":0.3,"rotation":338,"resourceUrl":"images/ba4202c2-845f-4b70-8167-5ef4ffb347bf","valoration":0}]}"""
val resp = Json.decodeFromString<BoardResponse>(json)
assertEquals(1, resp.posts.size)
}
I believe something must be wrong in how you setup Kotlinx Serialization as deserialization library. Please update your question with more details about how you use the library to deserialize this JSON, or open another question.
data class PostResponse(
val id: Int,
val x: Float,
val y: Float,
val rotation: Int,
val resourceUrl: String,
val valoration: Float
) : Serializable
Note: All variables names must be same as your response parameters(key)
Forget it, the only problem was the name of the val:
val posts: List
Just changed that to postList and it worked. No idea that the val name could be linked to the json name.
Issue Basics
ObjectBox version 2.5.1
Reproducibility: [always]
Hi, I am getting this error everytime I want to load my Object even though on saving I checked in the debugger and see that the relation target is not null and everything is saved correctly.
I am having those trhee entity classes
#Entity
data class NetflixCache (
val results: List<NetflixSearchResult>,
#Id var objectBoxId: Long?
) {
var searchParams: ToOne<SearchParams> = ToOne<SearchParams>(this, NetflixCache_.searchParams)
}
#Entity
data class SearchParams(val genrelist: String,
val type: String,
val imdbRatingMin: String,
val countryId: String,
val startYear: Int,
val endYear: Int,
val offset: Int? = 0,
val timeStamp: Long,
#Id var id: Long? = null)
#Entity
data class NetflixSearchResult (
val vtype: String?,
val img: String?,
val nfid: Long?,
val imdbid: String?,
val title: String?,
val clist: String?,
val poster: String?,
val imdbrating: Double?,
val synopsis: String?,
val titledate: Date?,
val year: Int?,
var id: Long,
#Id var objectBoxId: Long?
)
Using this code to save:
val cacheBox = LetsFlixApplication.boxStore.boxFor(NetflixCache::class.java)
val netflixCache = NetflixCache(searchResults, null)
netflixCache.searchParams.target = searchParams
cacheBox.put(netflixCache)
And this code to load:
val cachedResult = cacheBox.all //<-Exception is thrown here
As you can see in debugger when saving, ToOne Object is NOT null:
https://ibb.co/s3tdhqP
This is the exact exception message I am getting:
ToOne object is null inside com.haenles.letsflix.domain.NetflixCache
Issue for me was that data class was missing default values as recommended in the doc:
#Entity
data class User(
#Id var id: Long = 0,
var name: String? = null
)
I'm downloading few lists (RawAlbum, RawPhoto) from the web and I want to use properties from them to create another list. Then I want to use it to make a recyclerview.
So that's how it looks. It is the ListItem:
#Parcelize
data class ListItem(
val id: Int,
val title: String,
val albumTitle: String,
val thumbnailUrl: String
) : Parcelable
So I want to use the id and title from the RawAlbum. While the albumTitle and thumbnailUrl from the RawPhoto.
RawAlbum:
#Parcelize
#Entity(tableName = "albums")
data class RawAlbum(
#PrimaryKey
val id: Int,
val userId: Int,
val title: String
) : Parcelable
RawPhoto:
#Parcelize
#Entity(tableName = "photos")
data class RawPhoto(
#PrimaryKey
val id: Int,
val albumId: Int,
val title: String,
val url: String,
val thumbnailUrl: String
) : Parcelable