Im trying to decode this JSON with SwiftUI but I have this error below there is my code:
JSON
{
"page": 2,
"per_page": 6,
"total": 12,
"total_pages": 2,
"data": [
{
"id": 7,
"email": "michael.lawson@reqres.in",
"first_name": "Michael",
"last_name": "Lawson",
"avatar": "https://reqres.in/img/faces/7-image.jpg"
},
{
"id": 8,
"email": "lindsay.ferguson@reqres.in",
"first_name": "Lindsay",
"last_name": "Ferguson",
"avatar": "https://reqres.in/img/faces/8-image.jpg"
},
{
"id": 9,
"email": "tobias.funke@reqres.in",
"first_name": "Tobias",
"last_name": "Funke",
"avatar": "https://reqres.in/img/faces/9-image.jpg"
},
{
"id": 10,
"email": "byron.fields@reqres.in",
"first_name": "Byron",
"last_name": "Fields",
"avatar": "https://reqres.in/img/faces/10-image.jpg"
},
{
"id": 11,
"email": "george.edwards@reqres.in",
"first_name": "George",
"last_name": "Edwards",
"avatar": "https://reqres.in/img/faces/11-image.jpg"
},
{
"id": 12,
"email": "rachel.howell@reqres.in",
"first_name": "Rachel",
"last_name": "Howell",
"avatar": "https://reqres.in/img/faces/12-image.jpg"
}
],
"support": {
"url": "https://reqres.in/#support-heading",
"text": "To keep ReqRes free, contributions towards server costs are appreciated!"
}
}
DataManager
// MARK: - Menu
struct Menu: Codable {
let page, perPage, total, totalPages: Int
let data: [Datum]
let support: Support
enum CodingKeys: String, CodingKey {
case page
case perPage = "per_page"
case total
case totalPages = "total_pages"
case data, support
}
}
// MARK: - Datum
struct Datum: Codable {
let id: Int
let email, firstName, lastName: String
let avatar: String
enum CodingKeys: String, CodingKey {
case id, email
case firstName = "first_name"
case lastName = "last_name"
case avatar
}
}
// MARK: - Support
struct Support: Codable {
let url: String
let text: String
}
class DataManager: ObservableObject{
@Published var menu: [Menu] = []
func fetch(){
guard let url = URL(string: "https://reqres.in/api/users?page=2") else { return }
// var request = URLRequest(url: url)
let task = URLSession.shared.dataTask(with: url) {data, _, error in
guard let data = data, error == nil else { return }
do{
let menu = try JSONDecoder().decode([Menu].self, from: data)
DispatchQueue.main.async {
self.menu = menu
}
}catch{
print(error)
}
}
task.resume()
}
}
ContentView
import SwiftUI
struct ContentView: View {
@StateObject var profiles = DataManager()
var body: some View {
NavigationStack{
List{
ForEach(profiles.profile){ profile in
Text(profile.firstName)
}
}.onAppear{
DataManager().fetch()
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Could please anyone help me with that??
Your JSON file contains a single Menu
object, but your code is trying to decode it as an array. The type to decode should be Menu.self
rather than [Menu].self
.
Then maybe check if your menu
property should still be an array of Menu
that probably only ever has one element, or just a single Menu
.
Or if the JSON could actually contain multiple Menu
objects, then the server should always include the array brackets even when there happens to be only one.