如何将图像数组从 json 格式解析为 tabelview

Posted

技术标签:

【中文标题】如何将图像数组从 json 格式解析为 tabelview【英文标题】:how to parse images array from json format to tabelview 【发布时间】:2019-01-23 10:23:08 【问题描述】:

你好,我有如下的 json 数据


  "data" : [
    
      "resolved" : "N\/A",
      "type" : "Internal",
      "notes" : "test",
      "property_id" : "22",
      "regulation_item" : "8",
      "locations" : "Test",
      "id" : "45",
      "boiler_service_date" : "",
      "action" : "Further Investigation \/ Maintenance Required",
      "intervention_required" : "N\/A",
      "regulation_id" : "4",
      "regulation_item_name" : "Floors",
      "contravention" : "27",
      "images" : [
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_fzvXhnBwpw1547270966.png"
      ],
      "contraventions_item_name" : "Damaged wooden floors"
    ,
    
      "resolved" : "N\/A",
      "type" : "Internal",
      "notes" : "test",
      "property_id" : "22",
      "regulation_item" : "8",
      "locations" : "Ahmedabad",
      "id" : "47",
      "boiler_service_date" : "",
      "action" : "Maintenance Attention Required",
      "intervention_required" : "N\/A",
      "regulation_id" : "4",
      "regulation_item_name" : "Floors",
      "contravention" : "27",
      "images" : [
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_UNQbzzwTaK1547616334.png"
      ],
      "contraventions_item_name" : "Damaged wooden floors"
    ,
    
      "resolved" : "N\/A",
      "type" : "Internal",
      "notes" : "shhsge",
      "property_id" : "22",
      "regulation_item" : "8",
      "locations" : "Shsg",
      "id" : "57",
      "boiler_service_date" : "",
      "action" : "Maintenance Attention Required",
      "intervention_required" : "N\/A",
      "regulation_id" : "4",
      "regulation_item_name" : "Floors",
      "contravention" : "27",
      "images" : [
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/"
      ],
      "contraventions_item_name" : "Damaged wooden floors"
    ,
    
      "resolved" : "N\/A",
      "type" : "Internal",
      "notes" : "ewfewf",
      "property_id" : "22",
      "regulation_item" : "8",
      "locations" : "retested",
      "id" : "58",
      "boiler_service_date" : "",
      "action" : "Maintenance Attention Required",
      "intervention_required" : "N\/A",
      "regulation_id" : "4",
      "regulation_item_name" : "Floors",
      "contravention" : "27",
      "images" : [
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_8edaM4H6lZ1548233123.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_KhDXUhCHRN1548233123.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_zIhztQ2mVD1548233123.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_I3RWeD0VKl1548233123.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_VuDzsyR5gI1548233123.png"
      ],
      "contraventions_item_name" : "Damaged wooden floors"
    ,
    
      "resolved" : "N\/A",
      "type" : "Internal",
      "notes" : "sadsad",
      "property_id" : "22",
      "regulation_item" : "8",
      "locations" : "dads sad",
      "id" : "59",
      "boiler_service_date" : "",
      "action" : "Maintenance Attention Required",
      "intervention_required" : "N\/A",
      "regulation_id" : "4",
      "regulation_item_name" : "Floors",
      "contravention" : "27",
      "images" : [
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_n19NGjRfcH1548233550.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_WQn8zZeT0X1548233550.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_GYF21A9lMd1548233550.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_eaeoXVIPbU1548233550.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_w7KTfkTtdT1548233550.png"
      ],
      "contraventions_item_name" : "Damaged wooden floors"
    ,
    
      "resolved" : "N\/A",
      "type" : "Internal",
      "notes" : "sadsad",
      "property_id" : "22",
      "regulation_item" : "8",
      "locations" : "adds",
      "id" : "60",
      "boiler_service_date" : "",
      "action" : "Maintenance Attention Required",
      "intervention_required" : "N\/A",
      "regulation_id" : "4",
      "regulation_item_name" : "Floors",
      "contravention" : "27",
      "images" : [
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_fVghhQFhB31548233912.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_k3Y5CaQIsj1548233912.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_nVhXpintD51548233912.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_gS0wahnPzZ1548233912.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_TT2RZF2Pnv1548233912.png"
      ],
      "contraventions_item_name" : "Damaged wooden floors"
    ,
    
      "resolved" : "N\/A",
      "type" : "Internal",
      "notes" : "sadsad",
      "property_id" : "22",
      "regulation_item" : "8",
      "locations" : "sad sad",
      "id" : "61",
      "boiler_service_date" : "",
      "action" : "Further Investigation \/ Maintenance Required",
      "intervention_required" : "N\/A",
      "regulation_id" : "4",
      "regulation_item_name" : "Floors",
      "contravention" : "27",
      "images" : [
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_iy7hMFz69u1548234019.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_799ad8B4aj1548234019.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_5HFLk03Fgp1548234019.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_9zYgQKVqQ51548234019.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_UYf49scKwn1548234019.png"
      ],
      "contraventions_item_name" : "Damaged wooden floors"
    ,
    
      "resolved" : "N\/A",
      "type" : "Internal",
      "notes" : "sdsadas",
      "property_id" : "22",
      "regulation_item" : "8",
      "locations" : "daddy’s sad sad",
      "id" : "62",
      "boiler_service_date" : "",
      "action" : "Further Investigation \/ Maintenance Required",
      "intervention_required" : "N\/A",
      "regulation_id" : "4",
      "regulation_item_name" : "Floors",
      "contravention" : "27",
      "images" : [
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_htPOrErbgF1548234575.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_Y4ms5mkzgD1548234575.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_nuFk2NsfzV1548234575.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_IRpxGQb72s1548234575.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_N0w9sCwNcM1548234575.png"
      ],
      "contraventions_item_name" : "Damaged wooden floors"
    ,
    
      "resolved" : "N\/A",
      "type" : "Internal",
      "notes" : "sadsa",
      "property_id" : "22",
      "regulation_item" : "8",
      "locations" : "asdsadas",
      "id" : "63",
      "boiler_service_date" : "",
      "action" : "Further Investigation \/ Maintenance Required",
      "intervention_required" : "N\/A",
      "regulation_id" : "4",
      "regulation_item_name" : "Floors",
      "contravention" : "27",
      "images" : [
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_jJj0URkS9o1548234685.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_GU95bP9bhn1548234685.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_Arob8rq0QU1548234685.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_79Erayiurr1548234685.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_T8m2dxSmI91548234685.png"
      ],
      "contraventions_item_name" : "Damaged wooden floors"
    ,
    
      "resolved" : "",
      "type" : "Internal",
      "notes" : "Test test",
      "property_id" : "22",
      "regulation_item" : "26",
      "locations" : "Kitchen",
      "id" : "11",
      "boiler_service_date" : "0000-00-00",
      "action" : "Maintenance Attention Required",
      "intervention_required" : "",
      "regulation_id" : "6",
      "regulation_item_name" : "Gas",
      "contravention" : "202",
      "images" : [
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_j9QNiQ9Sun1548225475.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_zVBEjnkZf41548225475.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_LTaxAfGTZW1548225475.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_Mj5D6otfhX1548225475.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_DQTfucDObS1548225475.png"
      ],
      "contraventions_item_name" : "Condition and certs"
    ,
    
      "resolved" : "",
      "type" : "Internal",
      "notes" : "",
      "property_id" : "22",
      "regulation_item" : "35",
      "locations" : "",
      "id" : "6",
      "boiler_service_date" : "",
      "action" : "Maintenance Attention Required",
      "intervention_required" : "N\/A",
      "regulation_id" : "7",
      "regulation_item_name" : "Items",
      "contravention" : "210",
      "images" : [
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/"
      ],
      "contraventions_item_name" : "4 ring hob, oven and grill in good working order"
    ,
    
      "resolved" : "",
      "type" : "Internal",
      "notes" : "test test",
      "property_id" : "22",
      "regulation_item" : "35",
      "locations" : "",
      "id" : "7",
      "boiler_service_date" : "",
      "action" : "Extractor To Be Vented Out \/ Mechanical Fan To Be Installed",
      "intervention_required" : "N\/A",
      "regulation_id" : "7",
      "regulation_item_name" : "Items",
      "contravention" : "210",
      "images" : [
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_1Ot5YN8xL11548151400.png"
      ],
      "contraventions_item_name" : "4 ring hob, oven and grill in good working order"
    ,
    
      "resolved" : "",
      "type" : "Internal",
      "notes" : "test test",
      "property_id" : "22",
      "regulation_item" : "35",
      "locations" : "",
      "id" : "8",
      "boiler_service_date" : "",
      "action" : "Extractor To Be Vented Out \/ Mechanical Fan To Be Installed",
      "intervention_required" : "N\/A",
      "regulation_id" : "7",
      "regulation_item_name" : "Items",
      "contravention" : "210",
      "images" : [
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_9vhD62hRcp1548151402.png"
      ],
      "contraventions_item_name" : "4 ring hob, oven and grill in good working order"
    ,
    
      "resolved" : "",
      "type" : "Internal",
      "notes" : "Test Test",
      "property_id" : "22",
      "regulation_item" : "35",
      "locations" : "",
      "id" : "9",
      "boiler_service_date" : "",
      "action" : "Maintenance Attention Required",
      "intervention_required" : "N\/A",
      "regulation_id" : "7",
      "regulation_item_name" : "Items",
      "contravention" : "210",
      "images" : [
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_SbrrQxWl2A1548151619.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_ly1adD9T3C1548151620.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_qVup3MTUHh1548151620.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_4Asw2j3YF61548151620.png",
        "http:\/\/christies.buildingcontrolregister.ie\/webservice\/assets\/uploads\/inspection_photo\/img_y0FAbTekMh1548151620.png"
      ],
      "contraventions_item_name" : "4 ring hob, oven and grill in good working order"
    
  ],
  "total_count" : "1",
  "success" : "1",
  "message" : "Previous inspection list."

形成这个数据,我正在获取数据array,但无法在tableview 上显示,而且我无法理解如何获取图像数组表单,这里的数据是我用于填充我的tableview 的代码

代码

func PreviousInspeList()
    let preferences = UserDefaults.standard
    let uid = "u_id"
    let acTkn = "acc_tkn"

    let u_ID = preferences.object(forKey: uid)
    let A_Token = preferences.object(forKey: acTkn)

    let params = ["user_id": u_ID!, "access_token": A_Token!,"property_id": propertyID!]
    print(params)
    SVProgressHUD.show()
    Alamofire.request(previousinspectionslist, method: .post, parameters: params).responseJSON(completionHandler: (response) in
        switch response.result
        case.success(let value):
            let json  = JSON(value)
            print(json)
            let data = json["data"]
            data.array?.forEach( (iList) in
                let inspList = PreviousInspListModel(resolved: iList["resolved"].stringValue, type: iList["type"].stringValue, notes: iList["notes"].stringValue, property_id: iList["property_id"].stringValue, regulation_item: iList["regulation_item"].stringValue, locations: iList["locations"].stringValue, id: iList["id"].stringValue, boiler_service_date: iList["boiler_service_date"].stringValue, action: iList["action"].stringValue, intervention_required: iList["intervention_required"].stringValue, regulation_id: iList["regulation_id"].stringValue, regulation_item_name: iList["regulation_item_name"].stringValue, contravention: iList["contravention"].stringValue, contraventions_item_name: iList["contraventions_item_name"].stringValue)
                self.preInspData.append(inspList)
            )
            self.tblListView.reloadData()

            SVProgressHUD.dismiss()
        case.failure(let error):
            print(error.localizedDescription)
        

    )

这里是我的模型类

struct PreviousInspListModel 
    var resolved: String
    var type: String
    var notes: String
    var property_id: String
    var regulation_item: String
    var locations: String
    var id: String
    var boiler_service_date: String
    var action: String
    var intervention_required: String
    var regulation_id: String
    var regulation_item_name: String
    var contravention: String
    var contraventions_item_name: String

我只是想说明为什么我的数据没有出现在 tableview 上,以及如何获取图像 array 并显示在 tableview 上,请帮助我

【问题讨论】:

要显示所有图片吗? 是的,我想显示数组中的所有图像 请有人帮帮我 你能展示一下你的tableViewCell结构吗? pelase 检查我已添加并筛选我的 tableview 单元格结构 【参考方案1】:

UITableViewCell 中添加UICollectionView 以显示所有图像。并使用您的图像数组填充UICollectionView。找到在UITableView Cell 中添加UICollectionView 的精彩教程

https://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell-in-swift/

struct PreviousInspListModel 
    var resolved: String
    var type: String
    var notes: String
    var property_id: String
    var regulation_item: String
    var locations: String
    var id: String
    var boiler_service_date: String
    var action: String
    var intervention_required: String
    var regulation_id: String
    var regulation_item_name: String
    var contravention: String
    var contraventions_item_name: String
    var imageArray: [String]!

在你的循环中:

data.array?.forEach( (iList) in
                let inspList = PreviousInspListModel(resolved: iList["resolved"].stringValue, type: iList["type"].stringValue, notes: iList["notes"].stringValue, property_id: iList["property_id"].stringValue, regulation_item: iList["regulation_item"].stringValue, locations: iList["locations"].stringValue, id: iList["id"].stringValue, boiler_service_date: iList["boiler_service_date"].stringValue, action: iList["action"].stringValue, intervention_required: iList["intervention_required"].stringValue, regulation_id: iList["regulation_id"].stringValue, regulation_item_name: iList["regulation_item_name"].stringValue, contravention: iList["contravention"].stringValue, contraventions_item_name: iList["contraventions_item_name"].stringValue, imageArray: iList["images"].arrayObject as? [String])
                self.preInspData.append(inspList)
            )

【讨论】:

是的,但是如何从数据中获取图像数组 为模型中的图像定义一个数组。并在循环中在模型中投射数据时分配图像数组 请你说的更具体一些我无法理解 你能用数据填充tableView吗?? 是的,我成功填充了该图像数组的问题【参考方案2】:

Swift 4 发布后,Apple 推出了一种全新的 data encoding and decoding 协议方式,通过使您的自定义类型与一些易于采用的协议保持一致

1) 可编码 — 用于编码

2) 可解码 — 用于解码

3) Codable — 用于编码和解码

它还提供对类、结构和枚举的支持。

通过符合 Codable 协议,您必须将模型类创建为响应格式,无需手动解析数据。

在你的例子中:

struct ResponseData : Codable 
    let message: String
    let data : [PreviousInspListModel]


struct PreviousInspListModel : Codable 
    let resolved: String?
    let notes : String
    let type: String
    let images: [String]

在您的 Api 响应函数中:

Alamofire.request(previousinspectionslist, method: .post, parameters: params, encoding: JSONEncoding.default, headers: nil).responseData  (responseData) in
            responseData.result.ifSuccess 
                if let data = responseData.data
                    do 
                        let response = try JSONDecoder().decode(ResponseData.self, from: data) //Decode JSON Response Data
                        print(response)
                     catch let parsingError 
                        print("Error", parsingError)
                    
                
            
        

您可以找到有关swift 4 codable 的更多详细信息,或者只需在网络上搜索该术语。

【讨论】:

以上是关于如何将图像数组从 json 格式解析为 tabelview的主要内容,如果未能解决你的问题,请参考以下文章

如何在PHP中解析json

如何从android中的php关联数组解析JSON响应?

php怎么将数组数组转化为json格式的数据

IOS JSON解析包含多个数组

使用 OPENJSON 将 JSON 数组解析为表

如何将 alamofire 返回 json 解析为 Swift 中的字符串数组?