大家好我如何使用SiwftJson在swift中使用这种响应创建一个模型类我不知道如何包含这个
" total_songs":12, " title":" Arun Thapa", "结果":"成功", "最爱":" false", "封面":"艺术家/ 4096759431451532756hqdefault.jpg"在模型中使用以下响应
{
"total_favorite": 0,
"Data": [
[
{
"album_song_id": "120",
"artist_name": "Arun Thapa",
"status": "1",
"song_duration": "245213",
"song_favorite": "false",
"albumb_name": "Arun Thapa",
"created_by": "1",
"created_at": "0000-00-00 00:00:00",
"albumb_id": "42",
"chart_name": "Evergreen Songs",
"song_lyrics": "<html>\r\n<head>\r\n\t<title></title>\r\n</head>\r\n<body></body>\r\n</html>\r\n"
}
]
"total_songs": 12,
"title": "Arun Thapa",
"result": "success",
"favorite": "false",
"cover": "artists/4096759431451532756hqdefault.jpg"
}
提前致谢
答案 0 :(得分:0)
首先,您的JSON数据缺失&#34;],&#34;在#34;数据&#34;的末尾。 所以根据我的说法纠正JSON:
{
"total_favorite": 0,
"Data": [
[
{
"album_song_id": "120",
"artist_name": "Arun Thapa",
"status": "1",
"song_duration": "245213",
"song_favorite": "false",
"albumb_name": "Arun Thapa",
"created_by": "1",
"created_at": "0000-00-00 00:00:00",
"albumb_id": "42",
"chart_name": "Evergreen Songs",
"song_lyrics": "<html>\r\n<head>\r\n\t<title></title>\r\n</head>\r\n<body></body>\r\n</html>\r\n"
}
]
],
"total_songs": 12,
"title": "Arun Thapa",
"result": "success",
"favorite": "false",
"cover": "artists/4096759431451532756hqdefault.jpg"
}
我建议使用一个json To Swift模型转换器,你需要简单地放入JSON并获得快速的模型文件。 如果您不需要额外的代码,可以将其从快速模型文件中删除。
JSON与SWIFT模型转换器的链接 - http://www.json4swift.com
答案 1 :(得分:0)
这是一个如何将JSON映射到结构的示例。我希望你能从这段代码开始。
注意可选值和非可选值。
struct SomeDataModel {
let totalFavorites: Int
let totalSongs: Int
// other properties go here
init(withJson json: JSON) {
self.totalFavorites = json["total_favorites"].intValue
self.totalSongs = json["total_songs"].intValue
// initialization of other properties go here
}
}