Vishal Parmar
Vishal Parmar

Reputation: 613

how to parse images array from json format to tabelview

Plea

hello i have json data like below

{
  "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."
}

form this data i am getting data array but not able to display on tableview and also i am not able to understand how to fetch images array form that data here is my code for populating my tableview

Code

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

    })
}

here my model class

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
}

i just mean to tell why my data not is populating on tableview and how to get images array and also display on tableview please help me on this

Upvotes: 1

Views: 123

Answers (2)

Rocky
Rocky

Reputation: 3235

Swift 4 release, Apple unveiled a brand new way of data encoding and decoding protocols by conforming your custom types with some easy to adopt protocols

1) Encodable — for encoding

2) Decodable — for decoding

3) Codable — for both encoding as well as decoding

It provides support for class, struct and enum as well.

By conforming Codable protocol you have to create your model class as response format, no need to parse data manually.

In your example:

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

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

In your Api response function:

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

You can find more detail about swift 4 codable or just search the term on web.

Upvotes: 0

Ajay saini
Ajay saini

Reputation: 2470

Add UICollectionView inside UITableViewCell to show all the images. and use your Images Array for populating the UICollectionView. find the great tutorial for adding UICollectionView inside UITableView Cell

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]!
}

in your loop :

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

Upvotes: 1

Related Questions