मैं alamofire और Swiftyjson का उपयोग करके JSON को पार्स करना चाहता हूं
मैं इस तरह JSON (मान) प्राप्त करने का प्रयास करता हूं:
let headers: HTTPHeaders = [
"Authorization": "Basic Y2tfZTA1ZGNmMDkwNTNmODEyMGQwYTMyOGI4YzJkY2QzOTY5MmE5ZDAyNzpjc18zYzZiYWY2NTM0NDhkNDM4ZDM1ZDNmNDY5Nzg5ZGM2Y2VhZGRiZjNl",
"Accept": "application/json"
]
Alamofire.request("https://woo.demoapp.xyz/wp-json/wc/v2/products?category=15", headers: headers).responseJSON { response in
debugPrint(response)
if let json = response.result.value {
print("JSON: \(json)")
}
}
यहाँ woocommerce api से JSON डेटा है
[
{
"id": 29,
"name": "Sunglasses",
"permalink": "https://woo.demoapp.xyz/product/sunglasses/",
"description": "<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p>\n",
"images": [
{
"id": 17,
"src": "https://woo.demoapp.xyz/wp-content/uploads/2017/10/sunglasses.jpg",
"name": "Sunglasses",
}
],
मुद्दा यह है कि मैं एक सरणी को पॉप्युलेट करने में सक्षम नहीं हूं जो मैंने सोचा था कि जेएसओएन फ़ाइल को स्विफ्टीजसन के साथ टेबलव्यू में पार्स करने के लिए सही कोड होगा।
0
Irwan Madness
26 अक्टूबर 2017, 07:15
2 जवाब
सबसे बढ़िया उत्तर
कृपया इस प्रकार का उपयोग करें
let headers: HTTPHeaders = [
"Authorization": "Basic Y2tfZTA1ZGNmMDkwNTNmODEyMGQwYTMyOGI4YzJkY2QzOTY5MmE5ZDAyNzpjc18zYzZiYWY2NTM0NDhkNDM4ZDM1ZDNmNDY5Nzg5ZGM2Y2VhZGRiZjNl",
"Accept": "application/json"
]
Alamofire.request("https://woo.demoapp.xyz/wp-json/wc/v2/products?category=15", headers: headers).responseJSON { response in
debugPrint(response)
if let json = response.result.value {
print("JSON: \(json)")
let swjson = JSON(response.result.value!)
print(swjson)
// callback(swjson,nil)
var myMutableDictionary = [AnyHashable: Any]()
myMutableDictionary["myArray"] = swjson
let sss = JSON(myMutableDictionary as Any)
let arrdata = sss["myArray"].arrayObject
var productArray = NSArray()
productArray = arrdata as! [[String:AnyObject]] as NSArray
print(productArray.count)
yourtableview.reload()
}
}
// टेबल व्यू मेथड
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return productArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomeCell
let dic = productArray[indexPath.row] as! NSDictionary
let name = dic.object(forKey: "name") as! String
return cell
}
1
BHAVIK PANCHAL
26 अक्टूबर 2017, 11:44
Alamofire का उपयोग करके पैरामीटर के साथ छवि अपलोड करें
let parameters = ["category":"15" as AnyObject]
let headers: HTTPHeaders = [
"Authorization": "Basic Y2tfZTA1ZGNmMDkwNTNmODEyMGQwYTMyOGI4YzJkY2QzOTY5MmE5ZDAyNzpjc18zYzZiYWY2NTM0NDhkNDM4ZDM1ZDNmNDY5Nzg5ZGM2Y2VhZGRiZjNl",
"Accept": "application/json"
]
Alamofire.upload(multipartFormData: { multipartFormData in
multipartFormData.append(fileUrl , withName: "image" , fileName: yourfilename + ".yourfiletype", mimeType: "yourfiletype")
for (key, value) in parameters {
multipartFormData.append(value.data(using: String.Encoding.utf8.rawValue)!, withName: key)
}
},to: "https://woo.demoapp.xyz/wp-json/wc/v2/products", method: .post, headers: headers ,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.response { [weak self] response in
guard self != nil else {
return
}
}
upload.responseJSON { response in
let responseJSON = response.result.value as! NSDictionary
}
case .failure(let encodingError):
print("error:\(encodingError)")
}
})
0
BHAVIK PANCHAL
28 अक्टूबर 2017, 08:06
संबंधित सवाल
नए सवाल
swift3
इस टैग का उपयोग केवल ऐप्पल की स्विफ्ट प्रोग्रामिंग भाषा के संस्करण 3 में परिवर्तनों से सीधे संबंधित प्रश्नों के लिए करें। अधिक सामान्य भाषा के प्रश्नों के लिए टैग [स्विफ्ट] का उपयोग करें, या ऐप्पल प्लेटफार्मों पर विकसित होने के बारे में सवालों के लिए टैग [आईओएस], [कोको], [ऐप्पल-वॉच] आदि का उपयोग करें।