我正在尝试使用 SwiftyJSON 和 Alamofire 在 Swift 2.0 中打印 JSON
Alamofire.request(.GET, "http://announcement.vassy.net/api/AnnouncementAPI/Get/").responseJSON { (Response) -> Void in
//check if result has value
if let value = Response.result.value {
let json = JSON(value)
print(json)
}
}
它工作得很好,但是当试图访问一个特定的字符串时,会发生这种情况:
Alamofire.request(.GET, "http://announcement.vassy.net/api/AnnouncementAPI/Get/").responseJSON { (Response) -> Void in
//check if result has value
if let value = Response.result.value {
let json = JSON(value)
print(json["Body"].stringValue)
}
}
这是我从服务器获取的 JSON 文件的一小部分:
[
{
"InsertDate" : "2016-02-19T05:00:00",
"Title" : "Musical Theatre Yearbook Photo",
"Body" : "This is a yearbook photo reminder.",
"Id" : 34641
}
]
我已经为此工作了一段时间但无法弄清楚任何事情,我的直觉告诉我 JSON 没问题,这只是代码尝试打印它的方式。
最佳答案
由于外部对象是一个数组,因此您必须打印第一个项目的键值
print(json[0]["Body"].stringValue)
或者打印数组中的所有“body”
for anItem in json.array {
print(anItem["Body"].stringValue)
}
关于json - 使用 SwiftyJSON 打印以数组开头的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35512662/