SwiftyJson 解析 oData 响应
SwiftyJson parse oData response
我正在尝试使用 SwiftyJSON
解析 oData 网络服务
这是我的 oData 回复:
{
"odata.metadata":"http://url.com/odata/$metadata#Updates","value":[
{
"ID":1,"msgTitle":"Testing","reportedBy":"testUser"
}
]
}
这是我的 Swift 代码:
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
}
}
}
}
问题是 resData 返回空值。我在值数组中获取 JSON 做错了什么?
我也尝试过 swiftyJsonVar[0]["value"].arrayObject 但没有成功。
在查阅了 swiftyJSON 文档后,我能够使用以下语法解决这个问题:
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
}
}
}
}
}
我正在尝试使用 SwiftyJSON
解析 oData 网络服务这是我的 oData 回复:
{
"odata.metadata":"http://url.com/odata/$metadata#Updates","value":[
{
"ID":1,"msgTitle":"Testing","reportedBy":"testUser"
}
]
}
这是我的 Swift 代码:
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
}
}
}
}
问题是 resData 返回空值。我在值数组中获取 JSON 做错了什么?
我也尝试过 swiftyJsonVar[0]["value"].arrayObject 但没有成功。
在查阅了 swiftyJSON 文档后,我能够使用以下语法解决这个问题:
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
}
}
}
}
}