从JSON响应中解析两个不同对象的数组

时间:2018-04-24 12:39:12

标签: ios json swift xcode

我正在尝试解析JSON数据..我知道如何为一个对象数组做..但我不知道如何做一个响应,它必须有两个不同对象的数组..

例如这是JSON:

  {
"shifts": [
    {
        "id": 4,
        "region": "Eastren",
        "city": "Khobar",
        "nationality": "1",
        "id_service": 2,
        "shift_date": "2018-04-05",
        "shift_type": "night",
        "weekday": "sunday",
        "quantity_staff": 8,
        "lead_hours": 2,
        "created_at": "2018-04-23 11:46:20",
        "updated_at": "2018-04-24 08:46:14",
        "deleted_at": null
    },
    {
        "id": 5,
        "region": "Eastren",
        "city": "Khobar",
        "nationality": "Phili",
        "id_service": 2,
        "shift_date": "2018-04-04",
        "shift_type": "night",
        "weekday": "sunday",
        "quantity_staff": 8,
        "lead_hours": 2,
        "created_at": "2018-04-23 11:47:25",
        "updated_at": "2018-04-23 12:53:05",
        "deleted_at": "2018-04-23"
    }
],
"prices": [
    {
        "id": 1,
        "id_service": 2,
        "nationality": "Phili",
        "price": 150,
        "created_at": "2018-04-23 11:43:40",
        "updated_at": "2018-04-23 11:43:40",
        "deleted_at": null
    }
]
}

它有两个对象数组..转移和价格..如何解析它们中的每一个?

我的职能:

 func GetShiftsAndPrices(id: Int){

    let todosEndpoint: String = "my link"
    guard let todosURL = URL(string: todosEndpoint) else {
        print("Error: cannot create URL")
        return
    }
    var todosUrlRequest = URLRequest(url: todosURL)
    todosUrlRequest.httpMethod = "POST"
    todosUrlRequest.addValue("application/json", forHTTPHeaderField: "Content-Type")
    let newTodo: [String: Any] = ["id_service": id]
    let jsonTodo: Data
    do {
        jsonTodo = try JSONSerialization.data(withJSONObject: newTodo, options: [])
        todosUrlRequest.httpBody = jsonTodo
    } catch {
        print("Error: cannot create JSON from todo")
        return
    }

    let session = URLSession.shared

    let task = session.dataTask(with: todosUrlRequest) {
        (data, response, error) in
        guard error == nil else {
            print("error calling POST on /public/api/login_customer")
            print(error!)
            return
        }
        guard let responseData = data else {
            print("Error: did not receive data")
            return
        }

        // parse the result as JSON, since that's what the API provides
        do {

             //WHAT SHOULD I DO HERE?

                print("Success!")

        } catch  {
            print("error parsing response from POST")
            return
        }
    }
    task.resume()}
   }

我有轮班和价格的课程..并知道如果每个人都在答案中如何获得... ..:/ / p>

移:

                  let decoder = JSONDecoder()
                decoder.keyDecodingStrategy = .convertFromSnakeCase
                let shifts1 = try decoder.decode([Shifts].self, from: responseData)
                var shifts = [Shift]()
                for shift in shifts1{
                    let newshift = Shift(id: shift.id, region: shift.region, city: shift.city, nationality: shift.nationality, idService: shift.idService, shiftDate: shift.shiftDate, shiftType: shift.shiftType, weekday: shift.weekday, quantityStaff: shift.quantityStaff, leadHours: shift.leadHours)
                    shifts.append(newshift)
                }


                let userDefaults = UserDefaults.standard
                let encodedData: Data = NSKeyedArchiver.archivedData(withRootObject: shifts)
                userDefaults.set(encodedData, forKey: "shifts")
                userDefaults.synchronize()

                let decoded  = userDefaults.object(forKey: "shifts") as! Data
                let decodedShift = NSKeyedUnarchiver.unarchiveObject(with: decoded) as! [Shift]

                for shift in decodedShift {
                    print(shift.id)
                }

价格:

        let decoder = JSONDecoder()
        decoder.keyDecodingStrategy = .convertFromSnakeCase
        let prices1 = try decoder.decode([Prices].self, from: responseData)
        print("1")
        var prices = [Price]()
        for price in prices1{
            let newprice = Price(id: price.id, idService: price.idService,nationality: price.nationality, price: price.price)
            print("2")
            prices.append(newprice)
            print(newprice.nationality)
        }


        let userDefaults = UserDefaults.standard
        let encodedData: Data = NSKeyedArchiver.archivedData(withRootObject: prices)
        userDefaults.set(encodedData, forKey: "prices")
        userDefaults.synchronize()

        let decoded  = userDefaults.object(forKey: "prices") as! Data
        let decodedPrice = NSKeyedUnarchiver.unarchiveObject(with: decoded) as! [Price]

        for price in decodedPrice {
            print(price.nationality)
        }

如何在一个JSON响应中解析它们..我是新手...有人可以告诉我该怎么做吗?

2 个答案:

答案 0 :(得分:0)

let shifts = responseJson.valueFor(key:"shifts") as? [String] ?? []
let prices = responseJson.valueFor(key:"prices") as? [String] ?? []
print("\(shifts) and \(prices)")

答案 1 :(得分:0)

使用伞形结构

struct Root : Decodable {
    let shifts : [Shift]
    let prices : [Price]
}

以及两种不同的结构和价格结构:

struct Shift : Decodable {
    let id: Int
    let region, city, nationality : String
    let idService : Int
    let shiftDate, shiftType, weekday : String
    let quantityStaff, leadHours : Int
    let createdAt, updatedAt : String
    let deletedAt : String?
}

struct Price : Decodable {
    let id, idService : Int
    let nationality : String
    let price : Int
    let createdAt, updatedAt : String
    let deletedAt : String?
}

解码JSON写入

do {
    let decoder = JSONDecoder()
    decoder.keyDecodingStrategy = .convertFromSnakeCase
    let result = try decoder .decode(Root.self, from: responseData)
    print(result.prices)
    print(result.shifts)
} catch { print(error) }

此外,我建议将shiftTypeweekday等密钥直接解码为enum,例如

enum ShiftType : String, Decodable {
    case day, night
}

struct Shift : Decodable {
    let id: Int
    ...
    let shiftType : ShiftType
    ...