我的JSON结构如下。我是Alamofire和SwiftyJSON的新手。浏览文档并修补了几个小时之后,我一直无法弄清楚如何解析这个JSON结构中的各个元素。
{
"transactions": {
"transaction": [
{
"payment_id": 2,
"payment_date": "2015-06-30",
"url": "myurl",
"title": "mytitle",
"sell_id": 4,
"last_update": "2015-06-30",
"inventory_id": 4,
"amount": "30.00",
"item_id": 4682,
"buyer_id": 1
},
{
"payment_id": 1,
"payment_date": "2015-06-29",
"url": "myurl2",
"title": "mytitle",
"sell_id": 3,
"last_update": "2015-06-29",
"inventory_id": 3,
"amount": "40.00",
"item_id": 1061,
"buyer_id": 1
}
]
}
}
代码:
class func RecentTransactions() {
Alamofire.request(.GET, requestURL)
.responseJSON { (_, _, jsonData, _) in
println(jsonData!)
let json = JSON(jsonData!)
}
}
答案 0 :(得分:1)
外部两个对象是字典,内部对象是数组。
试试这个,它通过内部数组迭代并打印属性title
和url
的值(字典键有点令人困惑;-))
class func RecentTransactions() {
Alamofire.request(.GET, requestURL)
.responseJSON { (_, _, jsonData, _) in
println(jsonData!)
let json = JSON(jsonData!)
let transactions = json["transactions"]
let transaction = transactions["transaction"]
for (index: String, action: JSON) in transaction {
println(action["title"])
println(action["url"])
}
}
}