简体   繁体   中英

Retrofit: trouble parsing json

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
      },`
``

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM