将API响应字符串解析为JSON时出错。下面是我的JSON响应
“ \ t \ n {\”状态\“:0,\” msg \“:\”成功\“,\”数据\“:{\” numberOfOrder \“:3256,\” title_name \“:\\ URIBA DEALS \“,\” title_desc \“:\”您附近的商店和服务的交易和折扣\“,\”交易\“:[{\” DealID \“:\” 200 \“,\” BusinessID \“: \“ 279 \”,\“ DealType \”:\“特殊优惠\”,\“ business_type \”:\“蔬菜\”,\“ 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 \ “:\”洋葱@ 17卢比半公斤(从Amanora办事处摘洋葱)(每人限1公斤)\“,\” from_time \“:\” 00:00:00 \“,\” to_time \“:\ “ 00:00:00 \”,\“ from_date \”:\“ 0000-00-00 \”,\“ to_d ate \“:\” 0000-00-00 \“,\” deal_delivery_charges \“:\” 9 \“,\” app_Version \“:\” 0 \“,\” walletAmount \“:\” 0 \“, \“名称\”:\“ UriBA蔬菜Kharadi \”,\“ delivery_status_flag \”:\“ 0 \”,\“ delivery_days_after \”:\“ 1 \”,\“ business_address \”:\“ Kharadi,Pune \ “,\” min_order_amt \“:\” 12 \“,\”纬度“:\” 18.5578469 \“,\”经度\“:\” 73.9449945 \“,\” delivery_timings \“:\”上午9点至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")
}
}
}
我读过很多博客,但无法解决此问题。
我不熟悉iOS。
最佳答案
首先使用转换响应,let JSON = try! JSONSerialization.data(withJSONObject: yourResponseFromApi , options: JSONSerialization.WritingOptions.prettyPrinted)
转换api响应后,
然后您可以对其进行解码。let decode = try! JSONDecoder().decode(YourModelClass.self, from: JSON)