如何用这个 JSON 制作结构模型?我在 YouTube 上看过很多教程,但还是很困惑,在 struct 上使用 enum 是什么意思?
{
"err_code": 0,
"data": {
"message": "Success",
"result": {
"check_in": [
{
"created_at": "2022-06-20 10:00:37",
"late": "True",
"location": "Jl. H. Asmawi No.63, Beji, Kecamatan Beji, Kota Depok, Jawa Barat 16421, Indonesia",
"place": "GRIT OFFICE",
"folder": "./pegawai/grit/2022/06/20/masuk/10/00/",
"filename": "0000_rakha_fatih_athallah_picture.jpg",
"tujuan": "",
"link": "http://103.140.90.10:8081/face-recognition/client/python/pegawai/grit/2022/06/20/masuk/10/00/0000_rakha_fatih_athallah_picture.jpg"
},
{
"created_at": "2022-06-16 11:23:12",
"late": "True",
"location": "Jl. H. Asmawi No.63, Beji, Kecamatan Beji, Kota Depok, Jawa Barat 16421, Indonesia",
"place": "GRIT OFFICE",
"folder": "./pegawai/grit/2022/06/16/masuk/11/23/",
"filename": "0000_rakha_fatih_athallah_picture.jpg",
"tujuan": "",
"link": "http://103.140.90.10:8081/face-recognition/client/python/pegawai/grit/2022/06/16/masuk/11/23/0000_rakha_fatih_athallah_picture.jpg"
}
],
"check_out": []
}
}
}
这是我的结构模型。这很重要,因为我想在这个结构中使用
jsondecoder
。我想稍后使用 jsondecoder
。
import Foundation
struct jsonData : Codable{
var err_code : Int
var data : Data
enum CodingKeys: String, CodingKey {
case err_code = "err_code"
case data = "data"
}
}
struct Data: Codable{
var message : String
var result : Result
}
struct Result : Codable {
var check_in : [Checkin] = []
}
struct Checkin : Codable{
var created_at : String
var late : String
var location : String
var place : String
var folder : String
var tujuan : String
var link : String
enum CodingKeys: String, CodingKey {
case created_at = "created_at"
case late = "late"
case location = "location"
case place = "place"
case folder = "folder"
case tujuan = "tujuan"
case link = "link"
}
}
我认为您拥有的结构非常接近所需的结构,只需进行一些细微的调整,例如:
struct Response: Codable{
var err_code: Int
var data: DataObj
}
struct DataObj: Codable{
var message: String
var result: Result
}
struct Result: Codable {
var check_in: [Checkin]
}
struct Checkin: Codable, Hashable {
var created_at: String
var late: String
var location: String
var place: String
var folder: String
var tujuan: String
var link: String
}
像这样使用它:
struct ContentView: View {
@State var apiResponse: Response?
var body: some View {
VStack {
if let response = apiResponse {
Text("message: \(response.data.message) ")
Text("check_in.count: \(response.data.result.check_in.count) ")
List {
ForEach(response.data.result.check_in, id: \.self) { chk in
Text("created_at: \(chk.created_at) ")
}
}
}
}
.onAppear {
let jsonStr = """
{
"err_code": 0,
"data": {
"message": "Success",
"result": {
"check_in": [
{
"created_at": "2022-06-20 10:00:37",
"late": "True",
"location": "Jl. H. Asmawi No.63, Beji, Kecamatan Beji, Kota Depok, Jawa Barat 16421, Indonesia",
"place": "GRIT OFFICE",
"folder": "./pegawai/grit/2022/06/20/masuk/10/00/",
"filename": "0000_rakha_fatih_athallah_picture.jpg",
"tujuan": "",
"link": "http://103.140.90.10:8081/face-recognition/client/python/pegawai/grit/2022/06/20/masuk/10/00/0000_rakha_fatih_athallah_picture.jpg"
},
{
"created_at": "2022-06-16 11:23:12",
"late": "True",
"location": "Jl. H. Asmawi No.63, Beji, Kecamatan Beji, Kota Depok, Jawa Barat 16421, Indonesia",
"place": "GRIT OFFICE",
"folder": "./pegawai/grit/2022/06/16/masuk/11/23/",
"filename": "0000_rakha_fatih_athallah_picture.jpg",
"tujuan": "",
"link": "http://103.140.90.10:8081/face-recognition/client/python/pegawai/grit/2022/06/16/masuk/11/23/0000_rakha_fatih_athallah_picture.jpg"
}
],
"check_out": []
}
}
}
"""
// simulated response data
let data = jsonStr.data(using: .utf8)!
do {
self.apiResponse = try JSONDecoder().decode(Response.self, from: data)
print("\n---> apiResponse \(apiResponse)")
}
catch {
print(" error \(error)")
}
}
}
}
我们使用
CodingKeys
是为了对 JSON
中的名称使用自定义名称。
例如,您有
created_at
但您不想要下划线,您要做的就是将变量命名为 createdAt
并在 enum
中添加 case createdAt = "created_at"
至于解码部分,其实很简单:
do {
//let data = the json data you got.
let decoder = JSONDecoder()
let model = try decoder.decode(jsonData.self, from: data)
}catch {
print(String(describing: error))
}
请注意,我们在命名时不使用小写字母
structs, classes, actors & enums
。
编辑:感谢Marek R指出,有时
JSON
中的名称可以匹配特定于所使用语言的关键字(即:Swift
),因此有必要实现CodingKeys
。