Json数据快速解析

时间:2017-08-07 11:28:51

标签: ios arrays json swift swift3

是swift代码的新手

enter image description here

这是解析后我的json我必须在tableview上显示, 我可以正确地获取日期,详细信息,但是我无法获得" eventImage"在横幅图片中,我可以尝试但是没有得到那个请帮助我 这是我的代码
json调用函数

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()
                            }

我的tablview代码

  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
    }

如何显示&#34;横幅图像&#34; tableview

2 个答案:

答案 0 :(得分:1)

我认为问题在于您访问横幅图片的方式我觉得它应该像这样

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

其中i是eventImages数组的索引

在您的代码中,您可以执行此操作:

 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


                )
            )



    }        
   }         

}

答案 1 :(得分:0)

对我合作

 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
                                            ))

                                            }

                                            }
                                        }
                                    }


                                }