我想要一個僅包含以下模型類中的用戶名的陣列:
struct MyModel: Codable {
let status: Int?
let message: String?
let data: [MyModelClass]?
}
struct MyModelClass: Codable {
let userID, userName, email, emailCode: String?
let phone, password: String?
let image: String?
let dreamInfo: String?
enum CodingKeys: String, CodingKey {
case userID = "user_id"
case userName = "user_name"
case email
case emailCode = "email_code"
case phone, password, image
case dreamInfo = "dream_info"
}
}
我想要一個只有userNames的陣列。如何快速做到這一點?
我的模型回應如下:
{
"status": 1,
"message": "user found ",
"data": [
{
"user_id": "1",
"user_name": "ali",
"email": "[email protected]",
"email_code": "0",
"phone": "0",
"password": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
"image": "",
"dream_info": "this is my dream"
},
{
"user_id": "2",
"user_name": "raza",
"email": "[email protected]",
"email_code": "0",
"phone": "0",
"password": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
"image": "",
"dream_info": "this is my dream"
},
{
"user_id": "3",
"user_name": "usman",
"email": "[email protected]",
"email_code": "0",
"phone": "0",
"password": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
"image": "",
"dream_info": "this is my dream"
},
{
"user_id": "4",
"user_name": "haroon",
"email": "[email protected]",
"email_code": "0",
"phone": "0",
"password": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
"image": "",
"dream_info": "this is my dream"
}
]
}
我只想要這個模型的用戶名。這是我想要的輸出:
var myArray = ["ali", "raza", "usman", "haroon"]
如果模型類中有 10 個值,那么我需要myArray來擁有所有 10 個用戶名。
uj5u.com熱心網友回復:
對 Kudos 答案的簡單修改
var arr = []
for item in data {
arr.append(item.Username)
}
debugPrint(arr)
uj5u.com熱心網友回復:
這是作業代碼,在操場上測驗
import Foundation
let json: String = """
{
"status": 1,
"message": "user found ",
"data": [
{
"user_id": "1",
"user_name": "ali",
"email": "[email protected]",
"email_code": "0",
"phone": "0",
"password": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
"image": "",
"dream_info": "this is my dream"
},
{
"user_id": "2",
"user_name": "raza",
"email": "[email protected]",
"email_code": "0",
"phone": "0",
"password": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
"image": "",
"dream_info": "this is my dream"
},
{
"user_id": "3",
"user_name": "usman",
"email": "[email protected]",
"email_code": "0",
"phone": "0",
"password": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
"image": "",
"dream_info": "this is my dream"
},
{
"user_id": "4",
"user_name": "haroon",
"email": "[email protected]",
"email_code": "0",
"phone": "0",
"password": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
"image": "",
"dream_info": "this is my dream"
}
]
}
"""
struct MyModel: Codable {
let status: Int?
let message: String?
let data: [MyModelClass]?
}
struct MyModelClass: Codable {
let userID, userName, email, emailCode: String?
let phone, password: String?
let image: String?
let dreamInfo: String?
enum CodingKeys: String, CodingKey {
case userID = "user_id"
case userName = "user_name"
case email
case emailCode = "email_code"
case phone, password, image
case dreamInfo = "dream_info"
}
}
func loadJson() {
guard let data = json.data(using: .utf8) else {
print("Cant convert String to Data")
return
}
do {
let model = try JSONDecoder().decode( MyModel.self, from: data)
if let data = model.data {
let array = data.filter({$0.userName != nil}).map({$0.userName!})
print(array)
}
} catch {
print("Can't convert json to model")
}
}
loadJson()
結果在控制臺:

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/407619.html
標籤:
上一篇:Python如何從一個檔案中剪切一定數量的行并將它們寫入另一個檔案
下一篇:Python合并重疊路徑
