gpt4 book ai didi

json - 在 swift 中解析 A​​PI 对 JSON 的响应

转载 作者:行者123 更新时间:2023-11-30 10:32:12 25 4
gpt4 key购买 nike

将 API 响应字符串解析为 JSON 时出错。下面是我的 JSON 响应

"\t\n{\"status\":0,\"msg\":\"success\",\"data\":{\"numberOfOrder\":3256,\"title_name\":\"URIBA DEALS\",\"title_desc\":\"Deal & Discount from your nearly shops & services\",\"deals\":[{\"DealID\":\"200\",\"BusinessID\":\"279\",\"DealType\":\"specialoffer\",\"business_type\":\"vegetables\",\"ImageURL\":\"\",\"CategoryID\":\"1111\",\"categoryType\":\"\",\"ProductID\":\"0\",\"Priority\":\"-48\",\"Status\":\"1\",\"CreatedAt\":\"2019-11-19 07:57:32\",\"PromoCode\":\"\",\"Offer\":\"0\",\"OfferType\":\"0\",\"Branch\":\"Kharadi(EON)\",\"NewLogo\":\"http:\/\/aiotechnology.in\/AmanoraKatta\/AdminPanel\/deals_new_img\/1574148777.jpg\",\"userAcquisitionStatus\":\"0\",\"maxOrder\":\"0\",\"minOrder\":\"0\",\"userAcquisitionDiscount\":\"0\",\"dealFrom\":\"0000-00-00 00:00:00\",\"dealTo\":\"0000-00-00 00:00:00\",\"deal_description\":\"Onion @ Rs 17 half kg (Pick Onion From Amanora Office) ( limit 1 kg per person )\",\"from_time\":\"00:00:00\",\"to_time\":\"00:00:00\",\"from_date\":\"0000-00-00\",\"to_date\":\"0000-00-00\",\"deal_delivery_charges\":\"9\",\"app_Version\":\"0\",\"walletAmount\":\"0\",\"name\":\"UriBA Vegetables Kharadi\",\"delivery_status_flag\":\"0\",\"delivery_days_after\":\"1\",\"business_address\":\"Kharadi, Pune\",\"min_order_amt\":\"12\",\"latitude\":\"18.5578469\",\"longitude\":\"73.9449945\",\"delivery_timings\":\"9 AM to 9 PM\",\"CST\":\"0\",\"GST\":\"0\",\"DELI_CHRGS\":\"0\",\"PACK_CHRGS\":\"2\",\"displayimage\":\"1571221633.jpg\",\"logo_icon\":\"\",\"business_margin\":\"0\",\"business_margin_flag\":\"0\"}}]}}

下面是我的代码

Alamofire.request(url).responseString { response in
guard response.result.isSuccess,

let data = response.result.value else
{
print("Error while fetching tags: \(String(describing: response.result.error))")
completion(nil)
return
}

completion(data)

下面是我的模型类:

class DealModelRootClass : NSObject, NSCoding{

var data : DealDataModelRootClass!
var msg : String!
var status : Int!

init(fromJson json: JSON!){
if json.isEmpty{
return
}
let dataJson = json["data"]
if !dataJson.isEmpty{
data = DealDataModelRootClass(fromJson: dataJson)
}
msg = json["msg"].stringValue
status = json["status"].intValue
}


func toDictionary() -> [String:Any]
{
var dictionary = [String:Any]()
if data != nil{
dictionary["data"] = data.toDictionary()
}
if msg != nil{
dictionary["msg"] = msg
}
if status != nil{
dictionary["status"] = status
}
return dictionary
}


@objc required init(coder aDecoder: NSCoder)
{
data = aDecoder.decodeObject(forKey: "data") as? DealDataModelRootClass
msg = aDecoder.decodeObject(forKey: "msg") as? String
status = aDecoder.decodeObject(forKey: "status") as? Int
}


func encode(with aCoder: NSCoder)
{
if data != nil{
aCoder.encode(data, forKey: "data")
}
if msg != nil{
aCoder.encode(msg, forKey: "msg")
}
if status != nil{
aCoder.encode(status, forKey: "status")
}

}
}

我读了很多博客但无法解决这个问题。我是 swift iOS 新手。

最佳答案

首先使用 , 转换响应

让 JSON = 尝试一下! JSONSerialization.data(withJSONObject:yourResponseFromApi,选项:JSONSerialization.WritingOptions.prettyPrinted)

转换 api 响应后,然后你就可以解码它了。

让解码 = 尝试! JSONDecoder().decode(YourModelClass.self, 来自:JSON)

关于json - 在 swift 中解析 A​​PI 对 JSON 的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58930556/

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