gpt4 book ai didi

ios - Swift 3 - 如何使用 Alamofire 和 SwiftyJSON 获取 JSON 对象

转载 作者:可可西里 更新时间:2023-11-01 02:01:02 25 4
gpt4 key购买 nike

我想为我的 REST api 使用 Alamofire 和 SwiftyJSON。我已经访问了根 json,但我无法访问 json 的对象。结合这两个库正确解析它的最佳方法是什么?

我的 JSON:-

[
{
"ID": 2,
"Nm": "ABC",
"Descr": null,
"BeenCnt": "9",
"FavCnt": "9",
"ImgPath": "pathtoimage",
"TypeID": 4,
"Type": null,
"DayCnt": 5,
"NightCnt": 4,
"ValidDate": null,
"UsrCommentCnt": null,
"TourDates": null,
"CityList": [
{
"KeyID": null,
"ID": 1,
"Data": "abc"
},
{
"KeyID": null,
"ID": 12,
"Data": "abc"
}
],
"Seller": {
"ID": 1,
"Nm": "abc",
"CityNm": null,
"StateNm": null,
"CommentCnt": null,
"BeenCnt": null,
"Add": null,
"PackList": []
},
"Chrg": {
"ID": 0,
"PaxDetail": "Per Person",
"PkgMode": null,
"Amt": 15000,
"AmtCurrID": 2,
"AmtCurr": null,
"ChargeSeq": 0
},
"ItineraryList": []
}

我希望“Chrg”填充到我的 UICollectionView 中。

我在编码方面的尝试:-

Alamofire.request(url).responseJSON { (responseData) -> Void in


if((responseData.result.value) != nil) {
let swiftyJSONVar = JSON(responseData.result.value!)

if let resData = swiftyJSONVar[].arrayObject
{
self.arrRes = resData as! [[String:AnyObject]]
print(self.arrRes)
}

if self.arrRes.count > 0 {
self.collection.reloadData()
}

}
}

我的UICollectionViewCell:-

  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! PackagesCell
var dict = arrRes[(indexPath as NSIndexPath).row]
var charge = dict["Chrg"] as! Dictionary<String,String>
cell.amtLbl.text = charge["Amt"] as? String
cell.NmLbl.text = dict["Nm"] as? String
cell.nightsLbl.text = String(Int((dict["NightCnt"] as! Int64)))
cell.dayLbl.text = String(Int(dict["DayCnt"] as! Int64))

cell.perPersonLbl.text = dict["PaxDetail"] as? String
cell.validTillLbl.text = dict["ValidDate"] as? String
cell.iternaryListLbl.text = dict["CityList"]?["Data"] as? String
cell.cmtLbl.text = dict["BeenCnt"] as? String
cell.beenCmtLbl.text = dict["FavCnt"] as? String
self.id = String(dict["ID"] as! Int64)
let ImgPath = dict["ImgPath"] as? String
let fulURL = "\(self.site + ImgPath!)"
let newURL = fulURL.replacingOccurrences(of: "\\", with: "/")
let newwURL = newURL.replacingOccurrences(of: "png", with: "jpg")
cell.img.sd_setImage(with: URL(string:newwURL), placeholderImage: UIImage(named: "placeholder.png"))
return cell
}

最佳答案

我认为有更好的方法来解析 JSON

只需对其进行类型转换:

if let jsonArray = responseData.result.value as? Array<Dictionary<String,Any>> {
print("Json Response: \(jsonArray)") // serialized json response
self.arrRes = jsonArray
}
if let arrCityList = self.arrRes[0]["CityList"] as? Array<Dictionary<String,Any>> {
print(arrCityList)// You can populate the table from city list
}

关于ios - Swift 3 - 如何使用 Alamofire 和 SwiftyJSON 获取 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46314640/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com