Reputation: 110
I am trying to print JSON in Swift 2.0 using SwiftyJSON and Alamofire
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)
}
}
And it works perfectly fine, but when trying to access a specific string, this happens:
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)
}
}
And this is a small part of the JSON file I'm fetching from the server:
[
{
"InsertDate" : "2016-02-19T05:00:00",
"Title" : "Musical Theatre Yearbook Photo",
"Body" : "This is a yearbook photo reminder.",
"Id" : 34641
}
]
I've been working on this for a while and cannot figure anything out, my gut is telling me that the JSON is fine, it's just the way the code is trying to print it.
Upvotes: 1
Views: 1764
Reputation: 285220
Since the outer object is an array you have to print the value for a key of the first item
print(json[0]["Body"].stringValue)
Or printing all "bodies" in the array
for anItem in json.array {
print(anItem["Body"].stringValue)
}
Upvotes: 2
Reputation: 1673
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)
//Do the parsing
if !json.isEmpty
{
if let arrData = json.array
{
for dict in arrData
{
dict["Id"].int
dict["Title"].string
dict["Body"].string
dict["InsertDate"].string
//store this data into object add the object into array
}
}
}
}
}
Upvotes: 0