Matt Hammond Matt Hammond - 4 years ago 313
Swift Question

Alamofire JSON Request not pulling the data from a server

Im using Alamofire to pull a JSON file from a server (http://midlandgates.co.uk/JSON/reliveCacheData.json). To do this I have an Alamofire Request function which should pull the data down and write it to a class called

JSONDataClass
.

Alamofire Request

Alamofire.request(.GET, "http://midlandgates.co.uk/JSON/reliveCacheData.json")
.response { request, response, data, error in

if let data = data {
let json = JSON(data:data)

for locationData in json {
let locationDataJSON = JSONDataClass(json: locationData.1)
self.cacheData.append(locationDataJSON)
}

for title in self.cacheData {
print(title.memoryTitle)
}
}

}


However my printLn isn't printing the data from the JSON File which indicates there is a problem with either the request or the class. However I'm new to swift and can't seem to work out the problem, all help is appreciated!

Class

import Foundation
import CoreLocation
import SwiftyJSON

class JSONDataClass {

//Location MetaData
var postUser: String!
var memoryTitle: String!
var memoryDescription: String!
var memoryType: String!
var memoryEmotion: String!
var taggedFriends: String!

//Location LocationalData
var memoryLocation: CLLocationCoordinate2D

//MultiMedia AddressData
var media1: String!
var media2: String!
var media3: String!
var media4: String!
var media5: String!

//Writing to varibles
init(json: JSON) {
postUser = json["user"].stringValue
memoryTitle = json["title"].stringValue
memoryDescription = json["description"].stringValue
memoryType = json["type"].stringValue
memoryEmotion = json["emotion"].stringValue
taggedFriends = json["friends"].stringValue

memoryLocation = CLLocationCoordinate2D(latitude: json["latitude"].doubleValue, longitude: json["longitude"].doubleValue)

media1 = json["media1"].stringValue
media2 = json["media2"].stringValue
media3 = json["media3"].stringValue
media4 = json["media4"].stringValue
media5 = json["media5"].stringValue
}
}

Answer Source

Note: The author of the JSON content has fixed his JSON based on my answer so please don't downvote based on his newly valid JSON.

My answer was correct at the time of this post.


The problem is that you've got invalid JSON coming back from the server.

I switched your SwiftyJSON code out and wrapped it in a try-catch which illuminated the problem:

Alamofire.request(.GET, "http://midlandgates.co.uk/JSON/reliveCacheData.json")
            .response { request, response, data,  error in
                //print("status \(response)")

            do {

                let myData = try NSJSONSerialization.JSONObjectWithData(data!,
                    options: .MutableLeaves)

                print(myData)
            } catch let error {
                print(error)
            }
        }

And I get the following error:

2015-12-30 13:47:06.129 {{ignore}}[10185:2174510] Unknown class log in Interface Builder file. Error Domain=NSCocoaErrorDomain Code=3840 "Unescaped control character around character 961." UserInfo={NSDebugDescription=Unescaped control character around character 961.}

This error basically means that you have invalid JSON coming back from the server.

Make sure your json is valid by running it through JSONLint:

http://jsonlint.com

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download