I am facing some difficulties with parsing JSON - I have followed a tutorial for receiving data from SQL database. When I try to return and Array over to Swift it's ok, BUT I can't call any members of the Array.
Swift:
let myUrl = NSURL(string: "XXXXXXXXXXXXXXXX.Fr");
let request = NSMutableURLRequest(URL:myUrl!);
request.HTTPMethod = "POST";
// Compose a query string
let postString = "Pseudo=\(PseudoVar)";
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
if error != nil {
println("error=\(error)")
return
}
// You can print out response object
println("response = \(response)")
// Print out response body
let responseString = NSString(data: data, encoding: NSUTF8StringEncoding)
println("responseString = \(responseString)")
//Let's convert response sent from a server side script to a NSDictionary object:
var err: NSError?
var json = NSJSONSerialization.JSONObjectWithData(data, options: .MutableLeaves, error:&err) as? NSDictionary
if let parseJSON = json {
// Now we can access value of First Name by its key
var firstNameValue = parseJSON["firstName"] as? String
println("firstNameValue: \(firstNameValue)")
}
}
task.resume()
PHP (simplified):
<?php
array("Pseudo0" => "Hello", "Pseudo1" => "Good Morning");
echo json_encode($returnValue);
?>
Any advice would be helpful.