0

I am trying to get the title from json data but I am getting an error. Here is my model object

struct SpaceNewsModel: Identifiable, Codable {
    var id = UUID()
    
    let title: String
    let url: String
    let source: String
    
    
}

And when I get to this point in my networking, I get an error.

        let task = session.dataTask(with: request as URLRequest, completionHandler: { data, response, error in
            if error != nil {
                print(error!)
            } else {
                
                let decoder = JSONDecoder()
                if let safeData = data {
                    do {
                        let astroNews = try decoder.decode([SpaceNewsModel].self, from: safeData)
                        
                        print(astroNews[0].title)
                        
                    } catch {
                        print("DEBUG: Error getting news articles \(error.localizedDescription)")
                    }
                }
                
                
                
                let httpResponse = response as? HTTPURLResponse
//                print(httpResponse)
            }
        })
        
        task.resume()
    }

My main problem is with the line print(astroNews[0].title I am not formatting it right and I don't know how to access this data whether in an array or otherwise.

Here is the returned JSON data after making the request on the website postman.


[
    {
        "title": "Wales' new £2bn space strategy hopes",
        "url": "https://www.bbc.co.uk/news/uk-wales-60433763",
        "source": "bbc"
    },
    {
        "title": "Could Port Talbot become a centre of space tech? Video, 00:02:02Could Port Talbot become a centre of space tech?",
        "url": "https://www.bbc.co.uk/news/uk-wales-60471170",
        "source": "bbc"
    }
]


Normally there would be a title for each that I can access but here it is just the data. So to summarize, how would I get the first or second title in this JSON data? Because astroNews[0].title does not work.

EDIT: I want to clarify that if I were to decode an empty struct, I would not get any errors.

struct EMPTYSTRUCT: Codable {
    
    
}

But as soon as I add any variable like let title: String I get an error. I believe something is wrong in the formatting of the JSON because usually the JSON would look something this

{
    "coord": {
        "lon": -95.3633,
        "lat": 29.7633
    }
}

where I could name the struct "coord" however no such names exist in the returned JSON I'm working with. Only the variable names. The confusion is how would I construct my SpaceNewModel file to work with the returned JSON with no apparent object name.

Tomas
  • 135
  • 1
  • 6

1 Answers1

0

With reference to this answer.

Just change your structure to

struct SpaceNewsModel: Identifiable, Codable {
    let id = UUID()
    
    let title: String
    let url: String
    let source: String
}

This is not the solution but a workaround; if you want to solve the issue, refer to this answer

Asad
  • 279
  • 1
  • 7