Разбор массива JSON для сервера с Swift 3.0

Я пытаюсь отправить массив JSON на веб-сервер. Я просмотрел несколько примеров в Интернете, например https://www.youtube.com/watch?v=aTj0ZLha1zE&t, и сохранил CoreData на веб-сервере с Swift 3.0, который продемонстрировал, как анализировать данные, но я изо всех сил стараюсь это сделать.

Ниже приведена моя функция, которая должна отправлять данные на сервер:

func sendRecordToServer() -> [Record] { let fetchRequest = NSFetchRequest<NSDictionary>(entityName:"Record") fetchRequest.resultType = .dictionaryResultType do { let records = try context.fetch(fetchRequest) if let jsonData = try? JSONSerialization.data(withJSONObject: records, options: []) { // jsonData is a byte sequence, to view it you would need to convert to string print(String(bytes: jsonData, encoding: String.Encoding.utf8)) let URL_SAVE_DATA = URL(string: "http://localhost/api/postdata.php") let request = NSMutableURLRequest(url: URL_SAVE_DATA!) request.httpMethod = "POST" request.httpBody = jsonData let task = URLSession.shared.dataTask(with: request as URLRequest){ data, response, error in guard let data = data, error == nil else { // check for fundamental networking error print("error=\(String(describing: error?.localizedDescription))") return } let responseString = String(data: data, encoding: .utf8) print("responseString = \(String(describing: responseString))") } task.resume() } } catch { print("Error fetching data from CoreData") } return records } 

После кодирования данных в JSON он печатает следующим образом:

Необязательно ([["record_id": 8EC9C1C9-7DD4-4343-B7CC-E4615FDDA150, "name": John], ["record_id": 7EEA551D-9432-4737-99FB-6BFCF3A92D21, "name": Fred Smith]])

Однако, поскольку я пытаюсь разобрать его, хотя на сервер я получаю это, и ничто не отправляется на сервер:

responseString = Необязательный ("")

Обновить:

Следуя приведенному ниже комментарию, я вижу, как выглядит мой posdata.php:

 <?php //creating response array $json = file_get_contents('php://input'); //echo $json shoulkd show the json string $array = json_decode($json, true); // var_dump($arr) should show the array structure $response = array(); if($_SERVER['REQUEST_METHOD']=='POST'){ //getting values $record_id = $_POST['record_id']; $name = $_POST['name']; //including the db operation file require_once '../includes/DbOperation.php'; $db = new DbOperation(); //inserting values if($db->createTeam($record_id, $name)){ $response['error']=false; $response['message']='Record added successfully'; }else{ $response['error']=true; $response['message']='Could not add record'; } }else{ $response['error']=true; $response['message']='You are not authorized'; } echo json_encode($response); 

DBOperation:

 <?php class DbOperation { private $conn; //Constructor function __construct() { require_once dirname(__FILE__) . '/Config.php'; require_once dirname(__FILE__) . '/DbConnect.php'; // opening db connection $db = new DbConnect(); $this->conn = $db->connect(); } //Function to create a new user public function createTeam($record_id, $name) { $stmt = $this->conn->prepare("INSERT INTO record (record_id, name) values (?, ?)"); $stmt->bind_param("si", $record_id, $name); $result = $stmt->execute(); $stmt->close(); if ($result) { return true; } else { return false; } } } 

Solutions Collecting From Web of "Разбор массива JSON для сервера с Swift 3.0"