MaChee Neraid
MaChee Neraid

Reputation: 1043

How to convert JSON data into Kotlin object

I have a sample here of an existing json,
my issue is i need to convert it into kotlin,
I'm new in kotlin and been reading a lot of tutorial, but
i haven't since samples as complicated as this.

here's the JSON

{
"mobile": {
    "id":"1",
    "Device": {
        "Device Name": "Huawei P30 Pro",
        "Price": "10000",
        "rating": "3.4",
        "phone_img_url": "https://fdn2.gsmarena.com/vv/bigpic/huawei-mate30-pro-.jpg", 
        "Category": "Phablet",
        "Description": {
            "Network": ["GSM", "HSPA", "LTE"],
            "Launch": {
                "Announced Date": "2019, September",
                "Status": "Available. Released 2019, September"
            },
            "Body": {
                "Dimension": "158.1 x 73.1 x 8.8 mm (6.22 x 2.88 x 0.35 in)",
                "Weight": "198 g (6.98 oz)",
                "Build": "Front/back glass (Gorilla Glass 6), aluminum frame",
                "Network": [
                    "Single SIM (Nano-SIM) or Hybrid Dual SIM (Nano-SIM, dual stand-by)", 
                    "IP68 dust/water resistant (up to 1.5m for 30 mins)"],
                "Display":{
                    "Type": "OLED capacitive touchscreen, 16M colors",
                    "Size": "6.53 inches, 108.7 cm2 (~94.1% screen-to-body ratio)",
                    "Resolution": "1176 x 2400 pixels, 18.5:9 ratio (~409 ppi density)",
                    "Protection": ["Corning Gorilla Glass 6",
                        "DCI-P3",
                        "HDR10"
                    ]
                }
            }
        }
    },
    "id":"2",
    "Device": {
        "Device Name": "Xiaomi Redmi Note 8 Pro",
        "Price": "15000",
        "rating": "3.4",
        "Category": "Phablet",
        "Description": {
            "Network": ["GSM", "HSPA", "LTE"],
            "Launch": {
                "Announced Date": "2019, August",
                "Status": "Available. Released 2019, September"
            },
            "Body": {
                "Dimension": "161.4 x 76.4 x 8.8 mm (6.35 x 3.01 x 0.35 in)",
                "Weight": "200 g (7.05 oz)",
                "Build": "Front/back glass (Gorilla Glass 5)",
                "Network": [
                    "Hybrid Dual SIM (Nano-SIM, dual stand-by)"],
                "Display":{
                    "Type": "IPS LCD capacitive touchscreen, 16M colors",
                    "Size": "6.53 inches, 104.7 cm2 (~84.9% screen-to-body ratio)",
                    "Resolution": "1080 x 2340 pixels, 19.5:9 ratio (~395 ppi density)",
                    "Protection": ["Corning Gorilla Glass 6",
                        "500 nits max brightness",
                        "HDR"
                    ]
                }
            }
        }
    },
    "id":"3",
    "Device": {
        "Device Name": "Huawei P30 Pro",
        "Price": "17000",
        "rating": "3.4",
        "Category": "Phablet",
        "Description": {
            "Network": ["GSM", "HSPA", "LTE"],
            "Launch": {
                "Announced Date": "2019, March",
                "Status": "Available. Released 2019, March"
            },
            "Body": {
                "Dimension": "158 x 73.4 x 8.4 mm (6.22 x 2.89 x 0.33 in)",
                "Weight": "192 g (6.77 oz)",
                "Build": "Front/back glass, aluminum frame",
                "Network": [
                    "Single SIM (Nano-SIM) or Hybrid Dual SIM (Nano-SIM, dual stand-by)", 
                    "IP68 dust/water resistant (up to 2m for 30 mins)"],
                "Display":{
                    "Type": "OLED capacitive touchscreen, 16M colors",
                    "Size": "6.47 inches, 102.8 cm2 (~88.6% screen-to-body ratio)",
                    "Resolution": "1080 x 2340 pixels, 19.5:9 ratio (~398 ppi density)",
                    "Protection": ["DCI-P3",
                        "HDR10"
                    ]
                }
            }
        }
    }           
}

}

currently i do have a simple code but it doesn't return complete list of my "mobile"
here is my sample object

class Phone (
val mobile: PhoneList
)

data class PhoneList (
    val id: String,
    val device: DeviceInfo?
)

data class DeviceInfo (
    val device_name: String
)

thank you in advance for the help.

Upvotes: 3

Views: 6898

Answers (2)

Simon
Simon

Reputation: 1737

  1. Install this plugin: https://plugins.jetbrains.com/plugin/9960-json-to-kotlin-class-jsontokotlinclass-
  2. Restart Android studio
  3. Right-click on folder where you want your model class and select New > Kotlin data class File from JSON
  4. Past the JSON content and click OK
  5. Once you have the JSON mapped to model class you can use map to get the list of devices out. It will generate the model class automatically (essential timesaver).

Upvotes: 3

Phineas Huang
Phineas Huang

Reputation: 833

  1. Browser the website https://app.quicktype.io/
  2. Paste your JSON data to left column.
  3. Select the Source Type -> JSON
  4. Select the Serialization framework -> Just Types
  5. Choose your language you want(Kotlin)

You will get the model class type.

class Phone (
    val mobile: List<Mobile>
)

data class Mobile (
    val id: String,
    val device: Device
)

data class Device (
    val deviceName: String,
    val price: String,
    val rating: String,
    val category: String,
    val description: Description
)

data class Description (
    val network: List<String>,
    val launch: Launch,
    val body: Body
)

data class Body (
    val dimension: String,
    val weight: String,
    val build: String,
    val network: List<String>,
    val display: Display
)

data class Display (
    val type: String,
    val size: String,
    val resolution: String,
    val protection: List<String>
)

data class Launch (
    val announcedDate: String,
    val status: String
)

Upvotes: 9

Related Questions