I am trying to parse a oData web service using SwiftyJSON
Here is my oData response:
{
"odata.metadata":"http://url.com/odata/$metadata#Updates","value":[
{
"ID":1,"msgTitle":"Testing","reportedBy":"testUser"
}
]
}
Here is my Swift code:
Alamofire.request(URL, method: .get).responseString { (responseData) -> Void in
if((responseData.result.value) != nil) {
self.activityIndicator.stopAnimating()
let swiftyJsonVar = JSON(responseData.result.value!)
print(swiftyJsonVar)
if let resData = swiftyJsonVar["value"].arrayObject {
if let dict = resData as? [Dictionary<String, AnyObject>] {
for obj in dict {
let announce = announcement(fileDict: obj)
self.Announcements.append(announce)
}
self.tableView.reloadData()
self.tableView.isHidden = false
}
}
}
}
The problem is that resData is returning null. What I am doing wrong to get the JSON within the value array?
I have also tried swiftyJsonVar[0]["value"].arrayObject without success.
After consulting the swiftyJSON documentation, I was able to figure this out using the following syntax:
Alamofire.request(URL, method: .get).responseString { (responseData) -> Void in
if((responseData.result.value) != nil) {
self.activityIndicator.stopAnimating()
//log.info("Response: \(responseData.result.value)")
let jsonObj = responseData.result.value!
if let dataFromString = jsonObj.data(using: .utf8, allowLossyConversion: false) {
let json = JSON(data: dataFromString)
print(json)
if let resData = json["value"].arrayObject {
if let dict = resData as? [Dictionary<String, AnyObject>] {
for obj in dict {
let announce = announcement(fileDict: obj)
self.Announcements.append(announce)
}
self.tableView.reloadData()
self.tableView.isHidden = false
}
}
}
}
}