Correct JSON format for a Tableview

Hi,


I have been given an api which outputs the following


"{\"statusCode\": 200, \"headers\": {\"Access-Control-Allow-Origin\": \"*\"}, \"body\": [{\"ID\": 29, \"ColumnAID\": 12, \"ColumnB\": \"Blah\", \"ColumnC\": \"\", \"ColumnD\": \"Not Listed\", \"ColumnE\": null, \"ColumnF\": null, \"lat\": null, \"lon\": null}, {\"ID\": 22, \"ColumnAID\": 12, \"ColumnB\": \"Blah\", \"ColumnC\": \"\", \"ColumnD\": \"Canberra\", \"ColumnE\": \"ACT\", \"ColumnF\": null, \"lat\": null, \"lon\": null}, {\"ID\": 24, \"ColumnAID\": 12, \"ColumnB\": \"Blah\", \"ColumnC\": \"\", \"ColumnD\": \"Bondi Junction\", \"ColumnE\": \"NSW\", \"ColumnF\": null, \"lat\": null, \"lon\": null}, {\"ID\": 25, \"ColumnAID\": 12, \"ColumnB\": \"Blah\", \"ColumnC\": \"\", \"ColumnD\": \"Broadway\", \"ColumnE\": \"NSW\", \"ColumnF\": null, \"lat\": null, \"lon\": null}, {\"ID\": 26, \"ColumnAID\": 12, \"ColumnB\": \"Blah\", \"ColumnC\": \"\", \"ColumnD\": \"Castle Hill\", \"ColumnE\": \"NSW\", \"ColumnF\": null, \"lat\": null, \"lon\": null}]}"


My objective is to place the 9 columns in a Tableview.


Ive been following some tutorials that use JSONDecoder as well as URL Session.


My 1st question is around the start of this JSON. (ie before the \"body")


Will this effect JSONDecoder's ability to just output the results I need?


Thanks in advance


Todd

Here is my code


import UIKit

struct MyList: Decodable {
    let ID: Int
    let columnaID: Int
    let columnb: String
    let columnc: String
    let columnd: String
    let columne: String
    let columnf: String
    let lat: Double
    let lon: Double
}

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        let myapiurl = "https://xxx-api.ap-southeast-2.amazonaws.com/xxxxxxxxxxxxxx"
        let urlObj = URL(string: myapiurl)
        
        URLSession.shared.dataTask(with: urlObj!) {(data, response, error) in
            
            do {
                var List = try JSONDecoder().decode([MyList].self, from: data!)
                
                for columnb in List {
                    print(columnb)
                }
                
            } catch {
                print("We got an error")
            }
            
            }.resume()
    }
    
}


That is giving me error at line 27 Variable 'List' was never mutated; consider changing to 'let' constant

I have been given an api which outputs the following


You should better add how you have output that JSON, which prevents some sort of easy misunderstanding.


I assume it is the whole response from your API.

And with pretty-printed:

"""
{
    "statusCode": 200,
    "headers": {
        "Access-Control-Allow-Origin": "*"
    },
    "body": [
        {
            "ID": 29,
            "ColumnAID": 12,
            "ColumnB": "Blah",
            "ColumnC": "",
            "ColumnD": "Not Listed",
            "ColumnE": null,
            "ColumnF": null,
            "lat": null,
            "lon": null
        },
        {
            "ID": 22,
            "ColumnAID": 12,
            "ColumnB": "Blah",
            "ColumnC": "",
            "ColumnD": "Canberra",
            "ColumnE": "ACT",
            "ColumnF": null,
            "lat": null,
            "lon": null
        },
        {
            "ID": 24,
            "ColumnAID": 12,
            "ColumnB": "Blah",
            "ColumnC": "",
            "ColumnD": "Bondi Junction",
            "ColumnE": "NSW",
            "ColumnF": null,
            "lat": null,
            "lon": null
        },
        {
            "ID": 25,
            "ColumnAID": 12,
            "ColumnB": "Blah",
            "ColumnC": "",
            "ColumnD": "Broadway",
            "ColumnE": "NSW",
            "ColumnF": null,
            "lat": null,
            "lon": null
        },
        {
            "ID": 26,
            "ColumnAID": 12,
            "ColumnB": "Blah",
            "ColumnC": "",
            "ColumnD": "Castle Hill",
            "ColumnE": "NSW",
            "ColumnF": null,
            "lat": null,
            "lon": null
        }
    ]
}
"""


Outlined:

{
    "statusCode": 200,
    "headers": {...}
    "body": [...]
}


The outer most structure `{...}`is a JSON object, not an array. You may need to prepare something like this:

struct MyResponse: Decodable {
    let statusCode: Int
    let headers: [String: String]
    let body: [MyList]
}

struct MyList: Decodable {
    let ID: Int
    let columnaID: Int
    let columnb: String
    let columnc: String
    let columnd: String
    let columne: String?
    let columnf: String?
    let lat: Double?
    let lon: Double?
    
    enum CodingKeys: String, CodingKey {
        case ID
        case columnaID = "ColumnAID"
        case columnb = "ColumnB"
        case columnc = "ColumnC"
        case columnd = "ColumnD"
        case columne = "ColumnE"
        case columnf = "ColumnF"
        case lat
        case lon
    }
}


And try this:

            do {
                let myResponse = try JSONDecoder().decode(MyResponse.self, from: data!)
                let myList = myResponse.body
                
                for listItem in myList {
                    print(listItem.columnb)
                }
                
            } catch {
                print("We got an error")
            }

(You should better add error checking, but that's another issue.)

Correct JSON format for a Tableview
 
 
Q