Fetch JSOON object not array

Hi, I am able to get data in an array and loop through it but how can I get single object data and display it?

artwork view

import SwiftUI

struct ArtworkView: View {
  @StateObject var singleArtwork = SingleArtwork()
  var body: some View {
    NavigationView {
      List {
        ForEach(singleArtwork.artworksData, id: \.self) { artwork in
          HStack(spacing: 8) {
            URLImage(urlString: "https://melvynbiddulph.co.uk/images/gallery/"+artwork.file)
            Text(artwork.title)
              .bold()
            if (artwork.size != nil) {
              Text(artwork.size)
            }
          }
        }
      }
      .navigationTitle("Single Artwork")
      .onAppear {
        singleArtwork.fetch()
      }
    }
  }
}

struct ArtworkView_Previews: PreviewProvider {
  static var previews: some View {
    ArtworkView()
  }
}

Single Artwork Object

import SwiftUI

class SingleArtwork: ObservableObject {
  @Published var artworksData: [Artwork] = []
  func fetch() {
    guard let url = URL(string: "https://melvynbiddulph.co.uk/api/artwork/11") else {
      return
    }
     
    let task = URLSession.shared.dataTask(with: url) { data, response, error in
      guard let data = data, error == nil else {
        return
      }
       
      //convert to JSON
      do {
        let artworksData = try JSONDecoder().decode([Artwork].self, from: data)
        DispatchQueue.main.async {
          self.artworksData = artworksData
        }
      } catch {
        print(error)
      }
    }
    task.resume()
  }
}

URLImage

import SwiftUI

struct URLImage: View {
  let urlString: String
   
  @State var data: Data?
   
  var body: some View {
    if let data = data, let uiimage = UIImage(data: data) {
      Image(uiImage: uiimage)
        .resizable()
        .aspectRatio(contentMode: .fit)
        .frame(width: 130, height: 130)
        .background(Color.gray)
    } else {
      Image(systemName: "loading")
        .resizable()
        .aspectRatio(contentMode: .fit)
        .frame(width: 130, height: 130)
        .background(Color.gray)
        .onAppear {
          fetchData()
        }
    }
  }
   
  private func fetchData() {
    guard let url = URL(string: urlString) else {
      return
    }
     
  let task = URLSession.shared.dataTask(with: url) { data, response, error in
    self.data = data
    }
    task.resume()
  }
}

Obviously it works for an array of objects: https://www. melvynbiddulph .co.uk/api/artwork

but not working for single objects: https://www. melvynbiddulph .co.uk/api/artwork/11

but not working for single objects: https://www. melvynbiddulph .co.uk/api/artwork/11

  • What do you get ? What did you expect excatly ?
  • How is Artwork defined ?

Artwork

import Foundation
import SwiftUI

struct Artwork: Hashable, Codable {
  var size: String!
  var title: String!
  var file: String!
}

I get the following in browser but nothing in XCODE, how to debug?!:

{"id":11,"title":"NO NAME NO TITLE JUSTBORN","size":"806 X 1106 mm","category":"1","file":"1565686961MELV0011.jpg","artistNotes":"The painting is about the anguish of woman in post birth. She is of no nationality. Her hands (over large) and protective. Her belly still distended from the birth.","price":0,"isfeatured":0,"islive":1,"created_at":"2018-09-05T00:00:00.000000Z","updated_at":"2019-08-13T10:02:41.000000Z"}
Fetch JSOON object not array
 
 
Q