How to convert Alamofire response to [JSON] using Gloss?

I am replacing the SwifyJSON library with Gloss. I am having problems converting my WS response to JSON format. In SwiftyJSON, I did this as follows:

guard let data = response.result.value else  {
            ...
            return
        }

let jsonData = JSON(data)

My answer is as follows:

[{
  "lat": "45.2",
  "lon": "-79.38333",
  "name": "Sample"
}, {
  "lat": "23.43",
  "lon": "45.3",
  "name": "Sample"
}]

I need to create a JSON object array ([JSON]) from this so that I can use this method:

let jsonArray = ?
guard let destinations = [Destination].fromJSONArray(jsonArray) else
{
    ...
    return
}

I tried:

guard let data = response.result.value as? [(String,AnyObject)] else  {
            ...
            return
}

and

guard let data = response.result.value as? [Gloss.JSON] else  {
            ...
            return
}

First said: It is not possible to convert a value of type '[(String, AnyObject)]' into the expected type of argument '[JSON]' Second: An initializer for conditional binding must have an optional type, and not “[Destination]"

+4
source share
2 answers

"" "" . Bellow - , :

import Gloss
import Alamofire

...

Alamofire.request(.GET, url, parameters: params).responseJSON {
    response in switch response.result {
    case .Success(let json):
        // Here, stations is already an array of Station class
        let stations = [Station].fromJSONArray(json as! [Gloss.JSON])
        ...
    ...
}

Station:

import Gloss

class Station: Decodable {
    var id: Int = 0
    var attribute1: Int = 0
    var attribute2: Int = 0

    init(id: Int, attribute1: Int, attribute2: Int) {
        super.init()
        self.id = id
        self.attribute1 = attribute1
        self.attribute2 = attribute2
    }

    required init?(json: JSON) {
        super.init()
        self.id = ("id" <~~ json)!
        self.attribute1 = ("attribute1" <~~ json)!
        self.attribute2 = ("attribute2" <~~ json)!
    }
}
+1

Alamofire + Gloss .

( ), ( ):

//iOS 9.3, Xcode 7.3

/// stripped-down version
struct Event: Decodable {
    var id: String?

    init?(json: JSON) {
    id = "id" <~~ json
    }
}

//...

func getEvents(completion: (AnyObject?) -> Void) {
    request(.GET, baseURL + endpointURL)
        .responseJSON { response in
            switch response.result {
            case .Failure(let error):
                print(error.localizedDescription)
            case .Success:
                guard let value = response.result.value as? JSON,
                let eventsArrayJSON = value["events"] as? [JSON] //Thus spake the particular API docs
                else { fatalError() }
                let events = [Event].fromJSONArray(eventsArrayJSON)

                // do stuff with my new 'events' array of type [Events]
            }
        }
}

TL;DR

Gloss fromJSONArray [1] Alamofire .responseJSON [2].

[1] init?(json: JSON), Decodable.

[2], Gloss-y JSON, aka [String : AnyObject] -type.

, .:)

cocoapod: Alamofire-Gloss

0
source

All Articles