-4

I wrote the following function to do a GET request inside of my macOS app using Alamofire:

func getContent(room: String) -> String {
let scriptUrl = "https://klipped.in/api/\(room)"
Alamofire.request(scriptUrl).responseJSON { response in        
    if let json = response.result.value {
        print("\(json)") // serialized json response
    }
}
return "This should return the value of \"content\" in the json response"
}

I now want to parse the json in the most efficient way possible. I googled around ways to do this, but every solution I found seems overly complicated or doesn't run because of type problems.

Is there a simple way in Swift to access the values inside of a JSON without creating a Struct for every response? I'm thinking about something along the lines of:

get-json-value(json, "content")

That returns the string value of "content" inside of a json and null if it doesn't exist.

1 Answers1

0

The easiest way to parse a JSON in Swift 4 when using Alamofire is by using

getRoomData = JSON as NSDictionary
str = getRoomData["content"] as String

See an example at: AlamoFire GET api request not working as expected

An even Better way is using the Swifty JSON library:

let json = JSON(responseData.result.value!)
let roomContent = json["content"].string