如何快速解析数组的json数组
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何快速解析数组的json数组相关的知识,希望对你有一定的参考价值。
我是json解析的初学者,我无法解码和解析,请帮助我。
这里是我的模特
struct Welcome: Codable
let genreID, name, welcomeDescription, slug: String
let url: String
let videos: [Video]
struct Video: Codable
let videosID, title, videoDescription, slug: String
let release, isTvseries, runtime: String
let videoQuality: VideoQuality
let thumbnailURL, posterURL: String
和示例
[
"genre_id": "",
"name": "",
"description": "",
"slug": "",
"url": "",
"videos": [
"videos_id": "",
"title": "",
"description": "",
"slug": "",
"release": "",
"is_tvseries": "",
"runtime": "",
"video_quality": "",
"thumbnail_url": "",
"poster_url": ""
,
"videos_id": "",
"title": "",
"description": "",
"slug": "",
"release": "",
"is_tvseries": "",
"runtime": " ",
"video_quality": "",
"thumbnail_url": "",
"poster_url": ""
],
"genre_id": "",
"name": "",
"description": " ",
"slug": "",
"url": "",
"videos": [
"videos_id": "",
"title": " ",
"description": "",
"slug": "",
"release": "",
"is_tvseries": "",
"runtime": "",
"video_quality": "",
"thumbnail_url": "",
"poster_url": ""
,
"videos_id": "",
"title": "",
"description": "",
"slug": "",
"release": "",
"is_tvseries": "",
"runtime": "",
"video_quality": "",
"thumbnail_url": "",
"poster_url": ""
] ]
我想在响应块中使用JSONDecoder()
。我想通过completionHandler解析从响应到vc的json数据。和一些完成专家,请对此进行解释,以便我解决我的问题。
答案
从API /本地获取Data
后,您需要decode
如下所示的响应,
do
let data = //Data from the API
let welcomeList = try JSONDecoder().decode([Welcome].self, from: data)
welcomeList.forEach welcome in
welcome.videos.forEach video in
print(video.videosID)
catch
print(error)
要获取免费的解析和模型代码,您始终可以将JSON
放在此web app中。但是,您必须注意可选属性,默认情况下,模型属性保持为非可选,因此,如果您知道可以作为响应返回的任何属性nil
,请将其设置为可选(?)。
另一答案
class videosDetail: Codable
let genreId: String?
let name: String?
let welcomeDescription: String?
let slug: String?
let url: String?
let videos: [Video]?
enum CodingKeys: String, CodingKey
case genreId = "genre_id"
case name = "name"
case welcomeDescription = "description"
case slug = "slug"
case url = "url"
case videos = "videos"
// MARK: - Video
class Video: Codable
let videosId: String?
let title: String?
let videoDescription: String?
let slug: String?
let release: String?
let isTvseries: String?
let runtime: String?
let videoQuality: String?
let thumbnailUrl: String?
let posterUrl: String?
enum CodingKeys: String, CodingKey
case videosId = "videos_id"
case title = "title"
case videoDescription = "description"
case slug = "slug"
case release = "release"
case isTvseries = "is_tvseries"
case runtime = "runtime"
case videoQuality = "video_quality"
case thumbnailUrl = "thumbnail_url"
case posterUrl = "poster_url"
在常用功能中添加此方法
func getArrayViaCodable<T : Codable>(arrDict : [[String : Any]]) -> [T]?
if let jsonData = try? JSONSerialization.data(
withJSONObject: arrDict,
options: .prettyPrinted
)
do
let posts = try JSONDecoder().decode([T].self, from: jsonData)
return posts
catch
print(error)
return nil
else
return nil
通过上述方法传递字典数组
以上是关于如何快速解析数组的json数组的主要内容,如果未能解决你的问题,请参考以下文章