解析字典数组json的集合时,如下所示
{
"responsecode": "200",
"responsemsg": "Product List",
"total_pages_count": "35",
"data": [
{
"pk_productid": 412,
"fk_userid": 59,
"productname": "Cerulean",
"sku": "man-cerulean412",
"description": "",
"shortdescription": "",
"prodsaletype": 1,
"prodprice": 156000,
"is_approve": 1,
"issold": false,
"issoldprice": 0,
"isbid": 0,
"lastbidprice": 156000,
"isdiscount": false,
"isfixeddiscount": false,
"discountamt": 0,
"ispromocode": false,
"isonline": false,
"iscash": true,
"images": [
{
"imagepath": "http://www.artively.com/Upload/ProdColorThumbImage/270120161832_Cerulean_Fotor.jpg",
"imgvideotype": 1
}
],
"videos": [],
"first_name": "Manhar ",
"last_name": "Kapadiya",
"totalhour": 0,
"totalminute": 0,
"totalsecond": 0,
"is_auction_onhold": "1",
"bid_user_id": 0,
"bid_first_name": "",
"bid_last_name": "",
"bid_user_image": "Upload/ThubUserProfile/noimage.jpg",
"is_fav": 0,
"share_url": "http://www.artively.com/url/5dd129",
"share_count": 0,
"category_id": 0,
"category_name": "",
"issubmited": false
},
....
..
.
我能够获取图片网址和其他详细信息的值,但在UITableview上显示时,它不会显示在正确的索引上,例如
Tableview索引1 - 它显示产品名称和prod.price但图片网址显示在下一个tableview索引中,即-2
Tableview索引2 - 它显示第一个索引的图像网址为产品名称,价格为零
Tableview索引3 - 它显示产品名称和prod.price但图片网址显示在下一个tableview索引中,即-4
并继续这样......
这是我的代码
func parseJSONData(data: NSData) -> [ProductDetails] {
var product_Detail = [ProductDetails]()
do {
let jsonResult = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary
let jsonProductDetails = jsonResult?["data"] as! [AnyObject]
print("the json response is",jsonProductDetails)
for jsonproductDetail in jsonProductDetails{
let productDetail = ProductDetails()
productDetail.productAuthor = jsonproductDetail["first_name"]as! String
productDetail.productPrice = jsonproductDetail["prodprice"]as! Int
product_Detail.append(productDetail)
let jsonResult = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as? NSArray
let jsonProductImageDetails = jsonproductDetail["images"] as! [AnyObject]
print("the json response is",jsonProductImageDetails)
//Where to make a loop to get the desired result
for jsonproductImage in jsonProductImageDetails{
let product_Image = ProductDetails()
product_Image.productImages = jsonproductImage["imagepath"] as! String
product_Detail.append(product_Image)
}
}
}
catch {
print (error)
}
self.parseJSONImage(data)
return product_Detail
}
模型
class ProductDetails {
var productAuthor: String!
var productPrice: Int!
var artImages: String!
}
显示单元格图像
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! UserHomeScreenTableViewCell
// Configure the cell...
cell.artsAuthorName.text = globalArr[indexPath.row].productAuthor
cell.priceLabel.text = "\(globalArr[indexPath.row].productPrice)" //to convert string value to interger
let productDetailsObject = globalArr[indexPath.row].artImages
print("@@@@@@@@@@@@@@@@@",productDetailsObject)
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
if let url = NSURL(string: self.globalArr[indexPath.row].artImages.imagepath) {
if let data = NSData(contentsOfURL: url) {
if let image = UIImage(data: data) {
dispatch_async(dispatch_get_main_queue()) { () -> Void in
cell.artImageView.image = image
}
}
}
}
})
return cell
}
答案 0 :(得分:1)
模型类。
class ProductDetails {
var productAuthor: String!
var productPrice: Int!
var artImages: [ArtImage]!
}
class ArtImage {
var imagepath: String!
var imgvideotype: Int!
}
我们会将ArtImage
数组存储为ProductDetails
的属性。
Simillarly你也可以为 videos
创建其他类
所以你可以使用它如下。
let productDetailsObject = //value of your ProductDetails object
let imagePath = productDetailsObject.artImages[0].imagepath
重写该功能如下。
func parseJSONData(data: NSData) -> [ProductDetails] {
var product_Detail = [ProductDetails]()
do {
let jsonResult = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary
let jsonProductDetails = jsonResult?["data"] as! [AnyObject]
print("the json response is",jsonProductDetails)
for jsonproductDetail in jsonProductDetails{
let productDetail = ProductDetails()
productDetail.productAuthor = jsonproductDetail["first_name"]as! String
productDetail.productPrice = jsonproductDetail["prodprice"]as! Int
//we will fetch the images from first object, i.e. from the jsonproductDetail
let jsonProductImageDetails = jsonproductDetail["images"] as! [AnyObject]
var artImagesModelArray = [ArtImage]()
for image in jsonProductImageDetails {
let artImage = ArtImage();
artImage.imagepath = image["imagepath"] as! String
artImage.imgvideotype = image["imgvideotype"] as! Int
artImagesModelArray.append(artImage)
}
productDetail.artImages = artImagesModelArray;
product_Detail.append(productDetail)
}
}
catch {
print (error)
}
return product_Detail
}
答案 1 :(得分:0)
使用func parseJSONData(data:NSData)更改代码 - > [产品详情] { var product_Detail = ProductDetails 做{ 让jsonResult =尝试NSJSONSerialization.JSONObjectWithData(数据,选项: NSJSONReadingOptions.MutableContainers)为?的NSDictionary
let jsonProductDetails = jsonResult?["data"] as! [AnyObject]
for jsonproductDetail in jsonProductDetails {
let productDetail = ProductDetails()`
let jsonProductImageDetails = jsonProductDetails["images"] as! [AnyObject]
productDetail.productAuthor = jsonproductDetail["first_name"]as! String
product.Detail.productImages = jsonProductImageDetails["imagepath"] as! String
productDetail.productPrice = jsonproductDetail["prodprice"]as! Int
product_Detail.append(productDetail)
}
}
catch {
print (error)
}
self.parseJSONImage(data)
return product_Detail }