从 JSON 响应创建字符串数组 Alamofire
Posted
技术标签:
【中文标题】从 JSON 响应创建字符串数组 Alamofire【英文标题】:Create String Arrays from JSON Response Alamofire 【发布时间】:2018-08-22 21:01:51 【问题描述】:我正在尝试从 JSON 响应创建一些数组。我正在使用 Alamofire 对象映射器。我不确定我是否正确映射对象。
这是我的 JSON 响应
"status": "success",
"data": [
"id": 1,
"name": "EXERCISE",
"parent_id": null,
"children": [
"id": 2,
"name": "All Exercises",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:47",
"updated_at": "2018-08-22 09:03:47"
,
"id": 3,
"name": "Warmup",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:47",
"updated_at": "2018-08-22 09:03:47"
,
"id": 4,
"name": "Pliés",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:47",
"updated_at": "2018-08-22 09:03:47"
,
"id": 5,
"name": "Tendus (Slow)",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:47",
"updated_at": "2018-08-22 09:03:47"
,
"id": 6,
"name": "Tendus (Fast)",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:47",
"updated_at": "2018-08-22 09:03:47"
,
"id": 7,
"name": "Dégagés (Slow)",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:47",
"updated_at": "2018-08-22 09:03:47"
,
"id": 8,
"name": "Dégagés (Fast)",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:47",
"updated_at": "2018-08-22 09:03:47"
,
"id": 9,
"name": "Petits Battements",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:47",
"updated_at": "2018-08-22 09:03:47"
,
"id": 10,
"name": "Ronds de Jambe",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:47",
"updated_at": "2018-08-22 09:03:47"
,
"id": 11,
"name": "Adage",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:47",
"updated_at": "2018-08-22 09:03:47"
,
"id": 12,
"name": "Frappés",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:47",
"updated_at": "2018-08-22 09:03:47"
,
"id": 13,
"name": "Fondus",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:47",
"updated_at": "2018-08-22 09:03:47"
,
"id": 14,
"name": "Grands Battements",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:47",
"updated_at": "2018-08-22 09:03:47"
,
"id": 15,
"name": "Rises",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:47",
"updated_at": "2018-08-22 09:03:47"
,
"id": 16,
"name": "Stretch",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:47",
"updated_at": "2018-08-22 09:03:47"
,
"id": 17,
"name": "Tendus (Centre)",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:47",
"updated_at": "2018-08-22 09:03:47"
,
"id": 18,
"name": "Adage/PDB (Centre)",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:47",
"updated_at": "2018-08-22 09:03:47"
,
"id": 19,
"name": "Relevés",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:47",
"updated_at": "2018-08-22 09:03:47"
,
"id": 20,
"name": "Pirouettes",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:47",
"updated_at": "2018-08-22 09:03:47"
,
"id": 21,
"name": "Petit Allegro",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:47",
"updated_at": "2018-08-22 09:03:47"
,
"id": 22,
"name": "Medium Allegro",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:48",
"updated_at": "2018-08-22 09:03:48"
,
"id": 23,
"name": "Grand Allegro",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:48",
"updated_at": "2018-08-22 09:03:48"
,
"id": 24,
"name": "Men’s Allegro",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:48",
"updated_at": "2018-08-22 09:03:48"
,
"id": 25,
"name": "Traveling Centre (Odd Bits)",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:48",
"updated_at": "2018-08-22 09:03:48"
,
"id": 26,
"name": "Coda",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:48",
"updated_at": "2018-08-22 09:03:48"
,
"id": 27,
"name": "Révérence/Cool Down",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:48",
"updated_at": "2018-08-22 09:03:48"
],
"created_at": "2018-08-22 09:03:47",
"updated_at": "2018-08-22 09:03:47"
,
"id": 28,
"name": "LENGTH",
"parent_id": null,
"children": [
"id": 29,
"name": "All Lengths",
"parent_id": 28,
"children": [],
"created_at": "2018-08-22 09:03:48",
"updated_at": "2018-08-22 09:03:48"
,
"id": 30,
"name": "Short (__x4)",
"parent_id": 28,
"children": [],
"created_at": "2018-08-22 09:03:48",
"updated_at": "2018-08-22 09:03:48"
,
"id": 31,
"name": "Medium (__x8)",
"parent_id": 28,
"children": [],
"created_at": "2018-08-22 09:03:48",
"updated_at": "2018-08-22 09:03:48"
,
"id": 32,
"name": "Long (__x16)",
"parent_id": 28,
"children": [],
"created_at": "2018-08-22 09:03:48",
"updated_at": "2018-08-22 09:03:48"
,
"id": 33,
"name": "Marathon (__x32)",
"parent_id": 28,
"children": [],
"created_at": "2018-08-22 09:03:48",
"updated_at": "2018-08-22 09:03:48"
,
"id": 34,
"name": "Other",
"parent_id": 28,
"children": [],
"created_at": "2018-08-22 09:03:48",
"updated_at": "2018-08-22 09:03:48"
],
"created_at": "2018-08-22 09:03:48",
"updated_at": "2018-08-22 09:03:48"
]
这是我的模型类的 sn-p
var Lid: Int?
var Fid: Int?
var Lname: String!
var Fname: String!
public override func mapping(map: Map)
super.mapping(map: map)
Lid <- map["data.id"]
Lname <- map["data.name"]
Fid <- map["data.children.id"]
Fname <- map["data.children.name"]
当我调试时 - 我的模型类的属性为零。我怎么能接受这个响应并最终得到 Lid 和 Lname 数组?
【问题讨论】:
键children
的值也是一个数组(包含多个对象)。请注意 JSON 中的 []
。你必须重构你的模型。
@vadian 所以 data.children.id 不是访问 children 数组中 id 键的正确方法吗?
【参考方案1】:
你可以这样做
struct Exercise: Mappable
var id: Int?
var name: String?
var parent_id: Int?
var childrens: [Exercise]?
init?(map: Map)
mutating func mapping(map: Map)
id <- map["id"]
name <- map["name"]
parent_id <- map["parent_id"]
childrens <- map["children"]
然后像这样映射它们
func mapData(data: [String: Any])
//the data here is the JSON of yours
guard let exerciseArray = data["data"] as? [[String: Any]] else return
//now we should be able to map array
let mappedExercises = Mapper<Exercise>().mapArray(JSONArray: exerciseArray)
现在在新版本的 swift 中,您可以使用Codable
协议来处理您的模型的编码和解码。
【讨论】:
【参考方案2】:在 Swift 4 中,任何第三方对象映射器都已过时,因为内置的 (De)Codable
协议要强大得多。
JSON 包含一个根对象和一个具有相同数据结构的嵌套对象树。
这是带有Decodable
的独立版本,它甚至可以将日期解码为Date
实例
let jsonString = """
"status": "success",
"data": [
"id": 1,
"name": "EXERCISE",
"parent_id": null,
"children": [
"id": 2,
"name": "All Exercises",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:47",
"updated_at": "2018-08-22 09:03:47"
,
"id": 3,
"name": "Warmup",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:47",
"updated_at": "2018-08-22 09:03:47"
,
"id": 4,
"name": "Pliés",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:47",
"updated_at": "2018-08-22 09:03:47"
,
"id": 5,
"name": "Tendus (Slow)",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:47",
"updated_at": "2018-08-22 09:03:47"
,
"id": 6,
"name": "Tendus (Fast)",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:47",
"updated_at": "2018-08-22 09:03:47"
,
"id": 7,
"name": "Dégagés (Slow)",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:47",
"updated_at": "2018-08-22 09:03:47"
,
"id": 8,
"name": "Dégagés (Fast)",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:47",
"updated_at": "2018-08-22 09:03:47"
,
"id": 9,
"name": "Petits Battements",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:47",
"updated_at": "2018-08-22 09:03:47"
,
"id": 10,
"name": "Ronds de Jambe",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:47",
"updated_at": "2018-08-22 09:03:47"
,
"id": 11,
"name": "Adage",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:47",
"updated_at": "2018-08-22 09:03:47"
,
"id": 12,
"name": "Frappés",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:47",
"updated_at": "2018-08-22 09:03:47"
,
"id": 13,
"name": "Fondus",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:47",
"updated_at": "2018-08-22 09:03:47"
,
"id": 14,
"name": "Grands Battements",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:47",
"updated_at": "2018-08-22 09:03:47"
,
"id": 15,
"name": "Rises",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:47",
"updated_at": "2018-08-22 09:03:47"
,
"id": 16,
"name": "Stretch",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:47",
"updated_at": "2018-08-22 09:03:47"
,
"id": 17,
"name": "Tendus (Centre)",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:47",
"updated_at": "2018-08-22 09:03:47"
,
"id": 18,
"name": "Adage/PDB (Centre)",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:47",
"updated_at": "2018-08-22 09:03:47"
,
"id": 19,
"name": "Relevés",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:47",
"updated_at": "2018-08-22 09:03:47"
,
"id": 20,
"name": "Pirouettes",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:47",
"updated_at": "2018-08-22 09:03:47"
,
"id": 21,
"name": "Petit Allegro",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:47",
"updated_at": "2018-08-22 09:03:47"
,
"id": 22,
"name": "Medium Allegro",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:48",
"updated_at": "2018-08-22 09:03:48"
,
"id": 23,
"name": "Grand Allegro",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:48",
"updated_at": "2018-08-22 09:03:48"
,
"id": 24,
"name": "Men’s Allegro",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:48",
"updated_at": "2018-08-22 09:03:48"
,
"id": 25,
"name": "Traveling Centre (Odd Bits)",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:48",
"updated_at": "2018-08-22 09:03:48"
,
"id": 26,
"name": "Coda",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:48",
"updated_at": "2018-08-22 09:03:48"
,
"id": 27,
"name": "Révérence/Cool Down",
"parent_id": 1,
"children": [],
"created_at": "2018-08-22 09:03:48",
"updated_at": "2018-08-22 09:03:48"
],
"created_at": "2018-08-22 09:03:47",
"updated_at": "2018-08-22 09:03:47"
,
"id": 28,
"name": "LENGTH",
"parent_id": null,
"children": [
"id": 29,
"name": "All Lengths",
"parent_id": 28,
"children": [],
"created_at": "2018-08-22 09:03:48",
"updated_at": "2018-08-22 09:03:48"
,
"id": 30,
"name": "Short (__x4)",
"parent_id": 28,
"children": [],
"created_at": "2018-08-22 09:03:48",
"updated_at": "2018-08-22 09:03:48"
,
"id": 31,
"name": "Medium (__x8)",
"parent_id": 28,
"children": [],
"created_at": "2018-08-22 09:03:48",
"updated_at": "2018-08-22 09:03:48"
,
"id": 32,
"name": "Long (__x16)",
"parent_id": 28,
"children": [],
"created_at": "2018-08-22 09:03:48",
"updated_at": "2018-08-22 09:03:48"
,
"id": 33,
"name": "Marathon (__x32)",
"parent_id": 28,
"children": [],
"created_at": "2018-08-22 09:03:48",
"updated_at": "2018-08-22 09:03:48"
,
"id": 34,
"name": "Other",
"parent_id": 28,
"children": [],
"created_at": "2018-08-22 09:03:48",
"updated_at": "2018-08-22 09:03:48"
],
"created_at": "2018-08-22 09:03:48",
"updated_at": "2018-08-22 09:03:48"
]
"""
struct Root : Decodable
let status : String
let data : [Item]
struct Item : Decodable
let name : String
let id : Int
let parentId : Int?
let children : [Item]
let createdAt, updatedAt : Date
do
let data = Data(jsonString.utf8)
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
decoder.dateDecodingStrategy = .formatted(dateFormatter)
decoder.keyDecodingStrategy = .convertFromSnakeCase
let result = try decoder.decode(Root.self, from: data)
print(result)
catch print(error)
【讨论】:
【参考方案3】:https://github.com/schrockblock/eson
您可以尝试使用 Eson 库,它会直接将您的 json 转换为 Model 类的 Array。它在 swift 中进行 Json 序列化和反序列化。
【讨论】:
如果 OP 不知道如何映射数据,那么该工具就无关紧要了。以上是关于从 JSON 响应创建字符串数组 Alamofire的主要内容,如果未能解决你的问题,请参考以下文章
以 JSON 字符串数组形式获取 AFNetworking 响应的结果