我们知道,GsonFormat可以一键生成JavaBean
放弃 GsonFormat
Kotlin大势所趋,毋需多言。
再生成JavaBean已经不合时宜,更何况还有 Kotlin 数据类 的新特性似乎更加简洁。
也有很多支持Kotlin一键生成的
选择Generate Kotlin data classes from JSON
Generate Kotlin data classes from JSON 功能与 GsonFormat 一样,区别是可以一键生成Kotlin数据类,这样更适合我们用Kotlin开发
Install 的两种方式
方式一
直接打开 Plugins ,搜索 Gson ,就可以看到了,Install 插件即可
Generate Kotlin data classes from JSON
方式二 官网下载
方式一有可能会下载失败
下载失败
这种情况我遇到两次了,我一开始以为我的科学上网工具有问题,但是Twitter是可以正常使用的。
原因是公司的网络限制问题,我在家是可以下载安装的
第二种方式是可以通过官网下载
下载完后 > Preferences > Plugins > Install plugin from disk... > 选择已下载的jar包 > 导入即可
具体步骤请参考我的另一篇简书《Android Studio 配置主题》,就是遇到的这个问题,这里就不详述了。
使用
官网有详细介绍可以参考,我这边做一下展示
1 右击包名(你想要在哪个包下面生成数据类,就选择哪个包),点击Generate classe from JSON
点击 Generate classe from JSON
将 json 数据,拷贝到窗口,并输入将要生成的文件名,之后点击OK
左侧是根据 json 数据生成的类及子类
如果你不选择 Single File,将会按照左侧生成 11 个类,如下
image.png
选择 Single File ,并输入想要生成的文件名 Test,
选择 Single File ,并输入想要生成的文件名 Test
可以看到只生成了一个文件 Test.kt
可以看到只生成了一个文件 Test.kt
可以根据个人喜好来决定是否选择 Single File
附 案例中所使用的 json 数据
{
"code": 200,
"category": 5,
"result": [
{
"id": 550138132,
"type": 4,
"name": "Mack Daddy (Live)",
"copywriter": null,
"picUrl": null,
"canDislike": false,
"song": {
"name": "Mack Daddy (Live)",
"id": 550138132,
"position": 38,
"alias": [],
"status": 0,
"fee": 0,
"copyrightId": 0,
"disc": "1",
"no": 2,
"artists": [
{
"name": "卜凡",
"id": 13058488,
"picId": 0,
"img1v1Id": 0,
"briefDesc": "",
"picUrl": "http://p1.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg",
"img1v1Url": "http://p1.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg",
"albumSize": 0,
"alias": [],
"trans": "",
"musicSize": 0
}
],
"album": {
"name": "偶像练习生 表演曲目合集",
"id": 37579301,
"type": "EP/Single",
"size": 39,
"picId": 109951163170276615,
"blurPicUrl": "http://p1.music.126.net/0fNqjjWb3srRgcsb0w7Qzg==/109951163170276615.jpg",
"companyId": 0,
"pic": 109951163170276615,
"picUrl": "http://p1.music.126.net/0fNqjjWb3srRgcsb0w7Qzg==/109951163170276615.jpg",
"publishTime": 1522944000007,
"description": "",
"tags": "",
"company": "",
"briefDesc": "",
"artist": {
"name": "",
"id": 0,
"picId": 0,
"img1v1Id": 0,
"briefDesc": "",
"picUrl": "http://p1.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg",
"img1v1Url": "http://p1.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg",
"albumSize": 0,
"alias": [],
"trans": "",
"musicSize": 0
},
"songs": [],
"alias": [],
"status": 0,
"copyrightId": 0,
"commentThreadId": "R_AL_3_37579301",
"artists": [
{
"name": "偶像练习生",
"id": 13059577,
"picId": 0,
"img1v1Id": 0,
"briefDesc": "",
"picUrl": "http://p1.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg",
"img1v1Url": "http://p1.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg",
"albumSize": 0,
"alias": [],
"trans": "",
"musicSize": 0
}
],
"subType": "录音室版",
"transName": null,
"picId_str": "109951163170276615"
},
"starred": false,
"popularity": 100,
"score": 100,
"starredNum": 0,
"duration": 190914,
"playedNum": 0,
"dayPlays": 0,
"hearTime": 0,
"ringtone": null,
"crbt": null,
"audition": null,
"copyFrom": "",
"commentThreadId": "R_SO_4_550138132",
"rtUrl": null,
"ftype": 0,
"rtUrls": [],
"copyright": 2,
"transName": null,
"sign": null,
"hMusic": {
"name": "",
"id": 1449385115,
"size": 7639293,
"extension": "mp3",
"sr": 44100,
"dfsId": 0,
"bitrate": 320000,
"playTime": 190914,
"volumeDelta": -2
},
"mMusic": {
"name": "",
"id": 1449385116,
"size": 4583593,
"extension": "mp3",
"sr": 44100,
"dfsId": 0,
"bitrate": 192000,
"playTime": 190914,
"volumeDelta": -2
},
"lMusic": {
"name": "",
"id": 1449385117,
"size": 3055743,
"extension": "mp3",
"sr": 44100,
"dfsId": 0,
"bitrate": 128000,
"playTime": 190914,
"volumeDelta": -1
},
"mvid": 0,
"bMusic": {
"name": "",
"id": 1449385117,
"size": 3055743,
"extension": "mp3",
"sr": 44100,
"dfsId": 0,
"bitrate": 128000,
"playTime": 190914,
"volumeDelta": -1
},
"mp3Url": null,
"rtype": 0,
"rurl": null,
"privilege": {
"id": 550138132,
"fee": 0,
"payed": 0,
"st": 0,
"pl": 320000,
"dl": 320000,
"sp": 7,
"cp": 1,
"subp": 1,
"cs": false,
"maxbr": 999000,
"fl": 320000,
"toast": false,
"flag": 0,
"preSell": false
}
},
"alg": "featured"
}
]
}