简体   繁体   中英

Json data parse in swift

am new to swift code

在此处输入图片说明

this is my json after parsing I have to display on tableview, I can get the date, details,eventid properly but am not able to get the "eventImage" inside banner image I can try but am not getting that pls help me
this is my code
json calling function

if errorCode == "0" {


                            if let Media_list = jsonData["events"] as? NSArray {


                                for i in 0 ..< Media_list.count {

                                    if let MediaEventData = Media_list[i] as? NSDictionary {




       =====>>     Hear the problem    let imageURL = self.url+"/images/" + String(describing: MediaEventData["bannerImage"]!)

                                            self.Mediainfo.append(MediaEvent(
                                            eventId: MediaEventData["eventId"]as?String,
                                            date: MediaEventData["date"]as?String,
                                            eventname: MediaEventData["eventName"]as?String,
                                            bannerImages: imageURL


                                           )
                                        )
                                    }

                                }
                                self.do_table_refresh()
                            }

my tablview code

  override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Media", for: indexPath)as! MediaCustomTableViewCell

       let  row = indexPath.row


        let media = Mediainfo[row] as MediaEvent

        cell.DisplayDate.text = media.date

        cell.DisplayName.text = media.eventName


         cell.DisplayImage.image = UIImage(named: "profile_pic")
         cell.DisplayImage.downloadImageFrom(link: media.bannerImages!, contentMode: .scaleAspectFit)


        // Configure the cell...

        return cell
    }

how can display the "banner image" tableview

I believe problem is with the way you are accessing the banner image I think it should be like this

MediaEventData["eventImages"][i]["bannerImage"]

where i is index of your eventImages array

In your code you can do this:

 var imageUrl: String = ""
if let Media_list = jsonData["events"] as? [Any] {


    for i in 0 ..< Media_list.count {

        if let MediaEventData = Media_list[i] as? [String: Any] {

            let eventImages = MediaEventData["eventImages"] as! [[String: Any]]
                if eventImages.count > 0 {
                   let bannerImage = eventImages[0]["bannerImage"] as? String

                   imageUrl = self.url+"/images/" + String(describing: bannerImage!)

                }



            self.Mediainfo.append(MediaEvent(
                eventId: MediaEventData["eventId"]as?String,
                date: MediaEventData["date"]as?String,
                eventname: MediaEventData["eventName"]as?String,
                bannerImages: imageURL


                )
            )



    }        
   }         

}

Working fine to me

 if errorCode == "0" {


                            if let Media_list = jsonData["events"] as? NSArray {

                                self.Mediainfo.removeAll()


                                for i in 0 ..< Media_list.count {

                                    if let MediaEventData = Media_list[i] as? NSDictionary {

                                        if let image_list = MediaEventData["eventImages"] as? NSArray
                                         {

                                         for i in 0 ..< image_list.count
                                         {

                                            if let mydata = image_list[i] as? NSDictionary

                                            {
                                                let datavalue = mydata["bannerImage"]as? String
                                                self.imageurl = self.url+"/images/events/" + datavalue!

                                            self.Mediainfo.append(MediaEvent(
                                            eventId: MediaEventData["eventId"]as?String,
                                            date: MediaEventData["date"]as?String,
                                            eventname: MediaEventData["eventName"]as?String,
                                            bannerImages: self.imageurl
                                            ))

                                            }

                                            }
                                        }
                                    }


                                }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM