简体   繁体   中英

Create String Arrays from JSON Response Alamofire

I am trying to create some arrays from a JSON Response. I am using Alamofire Object Mapper. I'm not sure if I am mapping the object correctly.

Here is my JSON response

{
    "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"
        }
    ]
}

Here is snippet of my Model class

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"]
}

When I debug - the properties of my model class are nil. How could I take this response and end up with arrays of Lid and Lname?

you could do something like this

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"]
    }
}

And Map them like this

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)
}

Now in new version of swift you can use Codable protocol to handle the encoding and decoding your models.

In Swift 4 any third-party object mapper is obsolete because the built-in (De)Codable protocol is much more powerful.

The JSON contains a root object and a nested tree of objects with the same data structure.

This is a standalone version with Decodable , it is even able to decode the dates as Date instances

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) }
https://github.com/schrockblock/eson

You can try Eson library which will directly convert your json into Array of Model class. It does Json Serialization and deserialization in swift .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM