-1

After doing some tutorials iam trying to use a specific API but my POJO always get null object, what is the correct way to parse the data inside "results" key?

  "success": true,
  "metadata": {
    "sort": "POPULARITY",
    "total_products": 20,
    "title": "Phones & Tablets",
    "results": [
      {
        "sku": "1",
        "name": "Samsung Galaxy S9",
        "brand": "Samsung",
        "max_saving_percentage": 30,
        "price": 53996,
        "special_price": 37990,
        "image": "https://cdn2.gsmarena.com/vv/bigpic/samsung-galaxy-s9-.jpg",
        "rating_average": 5
      },`
``

vpedro72
  • 13
  • 7

1 Answers1

0

If you are using Retrofit and Gson

You can have the following POJOs

data class APIReponse(
    val success: Boolean,
    val metadata: Metadata
)

data class Metadata(
    val sort: String,
    val total_products: Int,
    val title: String,
    val results: List<Product>
)

data class Product(
    val sku: Int,
    val name: String,
    val brand: String,
    val max_saving_percentage: Int,
    val price: Int,
    val special_price: Int,
    val image: String,
    val rating_average: String
)

For more information on using Gson on Retrofit please check https://square.github.io/retrofit/

I hope that gives you a little idea.

Jaype Sison
  • 61
  • 1
  • 1