konnte ich die JSON-Daten in der Konsole drucken, die wie dieseWie speichere ich JSON-Daten aus einer POST-Anfrage mit Swifty Json in ein Wörterbuch?
{
"created_at" : "2016-07-21 20:46:53",
"name" : "PB Admin",
"id" : 1,
"updated_at" : "2016-07-21 12:46:53",
"lname" : "Admin",
"access_lvl" : 1,
"email" : "[email protected]",
"fname" : "PB"
}
, konnte aber nicht speichern Sie es in ein Wörterbuch aussieht.
Das Verfahren zur POST-Anfrage
private func makeHTTPPostRequest(path: String, body: NSDictionary) {
let request = NSMutableURLRequest(URL: NSURL(string: path)!)
// Set the method to POST
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.HTTPMethod = "POST"
do {
// Set the POST body for the request
let jsonBody = try! NSJSONSerialization.dataWithJSONObject(body, options: [])
request.HTTPBody = jsonBody
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
let json:JSON = JSON(data: data!)
print(json)//print on console
//i need to get the json data from here, but for some reason it would skip
//this method
self.parseJSON(json)
})
task.resume()
} catch {
// Create your personal error
//onCompletion(nil, nil)
}
}
Diese Methode sollte die JSON-Daten in ein Wörterbuch
private func parseJSON(json: JSON) {
for(_, object) in json {
let createdAt = object["created_at"].stringValue
let name = object["name"].stringValue
let id = object["id"].stringValue
let updatedAt = object["updated_at"].stringValue
let lName = object["lname"].stringValue
let accessLvl = object["access_lvl"].stringValue
let email = object["email"].stringValue
let fname = object["fname"].stringValue
let obj = ["createdAt":createdAt, "name":name, "id":id, "updatedAt":updatedAt, "lName":lName, "accessLvl":accessLvl, "email":email, "fname":fname ]
objects.append(obj)//wherein objects is a string dictionary
}
}
Jedes Mal, wenn ich den Code debuggen speichern, Objekten Wörterbuch immer null ist, auch wenn das verarbeitete Ganze fertig.