解析JSON数组时出现问题

时间:2019-05-03 16:26:19

标签: ios json swift alamofire swifty-json

我有这个JSON:

{
  "myData" : [
    [
      {
        "text" : "lorem ipsum",
        "id" : "myId"
      }
    ]
  ]
}

我想用SwiftyJSON获取“文本”和“ id”值。

我的代码:

    Alamofire.request(url, method: .post, parameters: parameters, encoding: URLEncoding(destination: .httpBody), headers: headers).responseJSON { (response) in
        switch response.result {
        case .success(let value):
            let json = JSON(value)
            let id = json //json["myData"]["id"]... how get "id" ?
            print(id)
    }

1 个答案:

答案 0 :(得分:2)

您似乎正在访问一个数组数组,因此需要像let id = json["myData"][0][0]["id"]下标所需的元素。