gpt4 book ai didi

php - 无法遍历此字典并组织成自定义结构

转载 作者:行者123 更新时间:2023-11-28 06:25:05 25 4
gpt4 key购买 nike

我有这段代码用于迭代数组 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 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 中构造它

我哪里错了?

最佳答案

我认为你的问题出在这行代码上

if let arr = value as? [[String:Any]] {

查看您的 JSON 文件,我可以看到您的 json 对象是这种格式:

["playerForm": {
1 = (
{
date = "2017-01-31";
name = Dicky;
result = L;
"results_id" = 42;
},
{
date = "2017-01-26";
name = Dicky;
result = L;
"results_id" = 41;
}
);...

所以你的 json 根对象是一个 [String : Any] 字典,你做对了。字典只有一对(键,值),键为“PlayerForm”。之后你假设那对的值是 [[String:Any]] 类型的,这将是一个字典数组的数组,我不认为它是:)我认为这部分:

2 =     (
{

在 JSON 文件中有点错误,我不太确定“2=”应该代表什么,但它不是数组,因为数组不包含键,如“1,2, 3……”。

我建议将值转换为其他值,也许可以尝试

某物 = 值(value)为? [字符串:任何]

如果可行,那么您可以将该字典中的值转换为 NSArray 以进行迭代。

只是和那里的 Actor 一起玩了一会儿:)

关于php - 无法遍历此字典并组织成自定义结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42176559/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com