json - 如何将 JSON 数据转换为 Kotlin 对象
问题描述
我这里有一个现有 json 的示例,
我的问题是我需要将它转换为 kotlin,
我是 kotlin 的新手,并且一直在阅读很多教程,但是
我没有像这样复杂的示例。
这是 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"
]
}
}
}
}
}
}
目前我确实有一个简单的代码,但它没有返回我的“手机”的完整列表
这是我的示例对象
class Phone (
val mobile: PhoneList
)
data class PhoneList (
val id: String,
val device: DeviceInfo?
)
data class DeviceInfo (
val device_name: String
)
提前感谢您的帮助。
解决方案
- 浏览网站https://app.quicktype.io/
- 将您的 JSON 数据粘贴到左列。
- 选择源类型 -> JSON
- 选择序列化框架 -> 只是类型
- 选择你想要的语言(Kotlin)
您将获得模型类类型。
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
)
推荐阅读
- python - Python open3D没有属性'create_coordinate_frame'
- java - Vector中的对象无法转换为int
- html - 使用css并排对齐两个div,div包含点图标和文本
- python - 通过 IPv6 使用 Scapy 的 DNS 请求
- microsoft-cognitive - 有没有合适的方法来训练一个没有表格边框的模型?
- c++ - 如何在 LLVM IR 中实现非标量别名?
- ibm-cloud - 文件未使用 python 上传到 IBM 云对象存储
- c# - 如何在 c# Windows UWP 中将数据添加到网格视图
- c++ - 创建一个类似 Q_PROPERTY 的宏并提取 __VA_ARGS__
- r - 强制 facet_grid 以与它们在数据集中出现的顺序相同的顺序绘制构面