我有这个代码片段,用于迭代数组playerForm
playerForm包含数组中每个播放器的多个数组,并包含每个播放器的形式。


["playerForm": {
1 =     (
            {
        date = "2017-01-31";
        name = Dicky;
        result = L;
        "results_id" = 42;
    },
            {
        date = "2017-01-26";
        name = Dicky;
        result = L;
        "results_id" = 41;
    }
  );
2 =     (
            {
        date = "2017-01-25";
        name = G;
        result = W;
        "results_id" = 38;
    },
            {
        date = "2017-01-25";
        name = G;
        result = D;
        "results_id" = 40;
    }
3 =     (
            {
        date = "2017-01-31";
        name = Sultan;
        result = W;
        "results_id" = 42;
    },
            {
        date = "2017-01-26";
        name = Sultan;
        result = W;
        "results_id" = 41;
    }
    );
    }]

这是我尝试使用的代码:
  let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:AnyObject]
                    print (json!)

                    if let dict = json?["playerForm"] as? [String:Any] {

                        print ("step 1")

                        for value in dict {
                            if let arr = value as? [[String:Any]] {
                                print(arr)
                                self.leagueForm = arr.flatMap { Form($0) }

                                for form in self.leagueForm {
                                    self.formGuide.append(form.player_result!)
                                }
                                print ("break")

                            }
                        }


                        print (self.formGuide)
                    }

这是我自定义的Struct来组织数据;
 struct Form {
    var player_result: String?
    var player_name: String?
    var result_date: String?
    var result_id: String?

    init(_ dictionary: [String : Any]) {
        self.player_result = dictionary["result"] as? String ?? ""
        self.player_name = dictionary["name"]  as? String ?? ""
        result_date = dictionary["date"]  as? String ?? ""
        result_id = String(dictionary["results_id"]  as? Int ?? 0)


    }
}

var leagueForm = [Form]()

但是,我收到警告:Cast from '(key: String, value: AnyObject)' to unrelated type [[String : Any]] always fails

这是我的PHP脚本,提供了初始数组:
$noPlayers = count($communityPlayersIds); //
$playerForm = array();
$playerForm = $dao->getCommunityForm($communityId, $noPlayers, $communityPlayersIds);

public function getCommunityForm($communityId, $noPlayers, $communityPlayersIds){
$sql = " SELECT IF(player1_id=?, player1_result, player2_result) AS result, IF(player1_id=?, player1_name, player2_name) AS name, date, results_id FROM `results` WHERE (player1_id=? OR player2_id=?) AND community_id=? ORDER BY date DESC Limit 8";
$stmt = $this->conn->prepare($sql);
$i = 0;
foreach ($communityPlayersIds as $cPI) {
    $i++;
    $stmt->bind_param("iiiii", $cPI, $cPI, $cPI, $cPI, $communityId);
    $stmt->execute();
    if ($result = $stmt->get_result()) {
        while($row = $result->fetch_array(MYSQLI_ASSOC)){
             $returnValue[$i][] = $row;
             }
        }
    }return $returnValue;
}

echo json_encode (array('playerForm' => $playerForm ));

因此,在上面的示例中,此1,2,3是我如何索引MySQL返回的结果。我这样做是为了分离数据,因此我可以在Swift中进行结构化

我要去哪里错了?

最佳答案

您没有正确解开JSON。 json["playerForm"]是字典的数组,而不是字典。尝试这个:

let json = try! JSONSerialization.jsonObject(with: data, options: [.allowFragments]) as! [String: Any]
var leagueForm = [Form]()

if let forms = json["playerForm"] as? [[String: Any]] {
    leagueForm = forms.flatMap { Form($0) }
}

我假设这是您的示例JSON:
{
    "playerForm": [
        {"result": "L", "name": "Dicky", "date": "2017-02-10", "results_id": 48},
        {"result": "L", "name": "Dicky", "date": "2017-02-10", "results_id": 47},
        {"result": "L", "name": "Dicky", "date": "2017-02-09", "results_id": 44},
        {"result": "L", "name": "Dicky", "date": "2017-01-16", "results_id": 32},
        {"result": "D", "name": "Dicky", "date": "2016-12‌​-12", "results_id": 4},
        {"result": "W", "name": "Dicky", "date": "2016-12-12", "results_id": 6},
        {"result": "W", "name": "Dicky", "date": "2016-12-12", "results_id": 8}
    ]
}

10-05 18:32