How can I parse JSON to convert this to an array of objects in Swift?

I would personally do this using NSArrays and NSDictionaries to reference each object in your JSON. Something like this should suffice (let JSON be your JSON variable)

if let array = JSON as? NSArray {
    for obj in array {
        if let dict = obj as? NSDictionary {
            // Now reference the data you need using:
            let id = dict.valueForKey("id")
        }
    }
}

This worked for me:

if let JSONData = try? Data(contentsOf: url!) {
    do {
        let parsed = try JSONSerialization.jsonObject(with: JSONData, options: .mutableContainers) as? NSArray
        for obj in parsed! {
            let obj = MyObject(json: obj as! NSDictionary)
            list.append(obj)
        }
    } catch let parseError {
        print(parseError)
    }
}

There is an easy way to do this in Swift 4.x But I had a difficult time getting to the simple answer so I'll post it here.

Step 1 : Create a class that matches your JSON and is derived from Codable. Here I arbitrarily name the class (that wraps your original JSON) as BlogPost.

You'll need to add an init method also

class BlogPost : Codable{
    var id : Int
    var day : Date
    var title : String
    var description : String

    init (id : Int, day : Date, title : String, description : String){
         self.id = id;
         self.day = day;
         self.title = title;
         self.description = description;
    }
}

Step 2 : add a load method that will load your array.

It will take a filename (pointing to file with json) and return an array of BlogPost ([BlogPost])

 func loadJson(filename fileName: String) -> [BlogPost]? {
        if let url = Bundle.main.url(forAuxiliaryExecutable: fileName) {
            do {
                let data = try Data(contentsOf: url)
                let decoder = JSONDecoder()
                let allBlogPosts = try decoder.decode([BlogPost].self, from: data)
                return allBlogPosts
            } catch {
                print("error:\(error)")
            }
        }
        return nil
    }

}

I think the tricky part is the type that you send to the decoder method. Since we want to decode an array of Decodable BlogPost objects we have to use: [BlogPost].self

decoder.decode([BlogPost].self, from: data)

Tags:

Ios

Json

Swift