SeppeDev
SeppeDev

Reputation: 2302

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

This is a sample of my json that is on my website:

[{
    "id": "1",
    "day": "Saturday 3/10",
    "title": "title1",
    "description": "lorem ipsum dolor sit amet consectetur adipiscing elit",
    "image": ""
},
{
    "id": "2",
    "day": "Saturday 10/10",
    "title": "title2",
    "description": "lorem ipsum dolor sit amet consectetur adipiscing elit",
    "image": ""
},
{
    "id": "3",
    "day": "Saturday 17/10",
    "title": "title3",
    "description": "lorem ipsum dolor sit amet consectetur adipiscing elit",
    "image": ""
}]

And now I want to save every object in an array of objects but it's possible that there are more than three elements. I think I have to use NSJsonSerialization because I have to get it from an url.

(I'm using swift 2)

Upvotes: 13

Views: 16376

Answers (4)

SeppeDev
SeppeDev

Reputation: 2302

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)
    }
}

Upvotes: 1

raddevus
raddevus

Reputation: 9077

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)

Upvotes: 9

Damyan Todorov
Damyan Todorov

Reputation: 566

Another way is with https://github.com/Hearst-DD/ObjectMapper, you can easly make objects from JSON and convert them back to JSON

class Event: Mappable {
    var id:String?
    var day:String?
    var title:String?
    var description:String?
    var image:String?

    required init?(map: Map){

    }

    func mapping(map: Map) {
        id             <- map["id"]
        day            <- map["day"]
        title          <- map["title"]
        description    <- map["description"]
        image          <- map["image"]
    }
}
// make event from JSON
let event = Mapper<Event>().map(JSON)
// to JSON
event.toJSON()

just simple example, I hope this will help to someone.

Upvotes: 0

Cailean Wilkinson
Cailean Wilkinson

Reputation: 1460

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")
        }
    }
}

Upvotes: 8

Related Questions