13

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)

4
  • 1
    how are you attempting to do this? (feel free to show some code), and what is the problem that your running into? Commented Dec 20, 2015 at 22:40
  • What's your actual question? Commented Dec 20, 2015 at 23:08
  • How I can save the objects from an online json file in an array Commented Dec 20, 2015 at 23:20
  • Have you looked at NSJSONSerialization? Commented Dec 20, 2015 at 23:59

4 Answers 4

9

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)
Sign up to request clarification or add additional context in comments.

1 Comment

As of swift 5, you don't need an explicit init in codable struct - compiler will gently generate it automatically
8

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

3 Comments

I think the question is how to Parse the JSON into arrays and dictionaries- say via NSJSONSerialization
This code works fine but wasn't directly what I wanted. But it helped me. Thank you!
@Cailean Wilkinson can you please explain let JSON be your JSON variable? what will be the value of JSON variable
1

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

Comments

0

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.

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.