我的Swift代码中有一个HTTP请求,它向我的PHP代码发送POST请求

class Comment
{
    public $CommenterName;
    public $CommentDate;
    public $CommentLikes;
    function __construct($CommenterName,$CommentDate,$CommentLikes)
    {
        $this->CommenterName = $CommenterName;
        $this->CommentDate = $CommentDate;
        $this->CommentLikes = $CommentLikes;
    }
}

我的php代码返回一个包含Comment对象的数组
  let jsonData = try JSONSerialization.jsonObject(with: data, options: []) as! [Any]
  print(jsonData[0])

打印这个返回我
{
    CommentDate = "2017-06-29 01:21:57";
    CommentLikes = 2;
    CommenterName = muradsh;
}

当我想访问这样的对象时
           print(jsonData[0][2])

或者这个
 print(jsonData[0]["CommenterName"])

它返回以下错误Type 'Any' has no subscript members
如何访问jsonData中的CommenterName

最佳答案

由于您得到的数据实际上是一个对象数组,请尝试按如下方式进行转换:

let jsonData = try JSONSerialization.jsonObject(with: data, options: []) as! [[String: Any]]

10-08 02:44