2

I want to easily decode a JSON file with Decode protocol from Swift 4 on Xcode 9. This my question : How to decode à JSON like this:

[
  {
    "name": "My first Catalog",
    "order": 0,
    "products": [
      {
        "product": {
          "title": "product title",
          "reference": "ref"
        }
      }
    ]
  }
]

I try this but it's doesn't work

fileprivate struct Catalog: Codable {
    var name: String
    var order: Int
    var product: [Product]
}

fileprivate struct Product: Codable {
    var title: String
    var reference: String
}

...

// JSON Decoder
        do {
            let jsonData = try Data(contentsOf: URL(fileURLWithPath: filePath), options: .alwaysMapped)

            let jsonDecoder = JSONDecoder()

            let jsonCatalogs = try? jsonDecoder.decode(Array<Catalog>.self,
                                                       from: jsonData)


            return jsonCatalogs

        } catch {
            print ("error")
            return nil
        }

I don't know why it doesn't work in Swift 4 with Xcode 9. Thank for your help ;-)

2 Answers 2

8

actually the thing is that your structs are wrong,

    fileprivate struct Catalog: Codable {
        var name: String
        var order: Int
        var products: [Products] // this key is wrong in your question, it should be products instead of product
    }

    //this particular structure was missing as your products is having a dictionary and in that dictionary you are having product at key product
    fileprivate struct Products: Codable {
        var product: Product
    }
    fileprivate struct Product: Codable {
        var title: String
        var reference: String
    }

now you can check your fucntion and also you can easily debug it using try catch with error handlings

...

// JSON Decoder

  do {
        let jsonData = try Data(contentsOf: URL(fileURLWithPath:filePath), options: .alwaysMapped)

        let jsonDecoder = JSONDecoder()

        let jsonCatalogs = try? jsonDecoder.decode(Array<Catalog>.self,from: jsonData)
        print(jsonCatalogs)
        return jsonCatalogs

    } catch let error {
        print ("error -> \(error)") // this will always give you the exact reason for which you are getting error
        return nil
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @Er. Khatri, ;-)
@iLandes if it works for you kindly accept the answer, kudos
-3

Try this solution according to your JSON and i have worked with Decoder in swift 4 with xcode 9.4.

    struct Catalog : Decodable {
        let name : String?
        let order : Int?
        let productArray : [Products]? // this is products array.
 }

    struct Products : Decodable {
        let productDict : Product? // this is product dictionary
 }
    struct Product : Decodable {
        let title : String?
        let reference : String? 
}
 var catalogArray = [Catalog]() // Declaration

    // JSON Decoder   
do {
            let jsonData = try Data(contentsOf: URL(fileURLWithPath:filePath), options: .alwaysMapped)
            let jsonDecoder = JSONDecoder()
            let jsonCatalogs = try? jsonDecoder.decode(Catalog.self,from: jsonData)
            return jsonCatalogs
        } catch let error {
            print ("error -> \(error)") // this will always give you the exact reason for which you are getting error
            return nil
        }

*

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.