简体   繁体   中英

Parsing JSON from NSData with Swift

I want to parse data in array but I can't. Please let me know if you have any suggestion. Here is my data sample:

[{"ListSequence":1,"StarTime":"15.06.2016 09:00","WorkTime":15,"EndTime":"15.06.2016 09:15","Appointment":"","CustomerId":"12345","Name":"","Address":"bla bla"}, {"ListSequence":2,"StarTime":"15.06.2016 09:20","WorkTime":15,"EndTime":"15.06.2016 09:35","Appointment":"","CustomerId":"54321","Name":"","Address":"bla bla"}]

This is my function:

func parserJSON(data: NSData){
    var names = [String]()
    do {
        if let ipString = NSString(data:data, encoding: NSUTF8StringEncoding) {
            let jsonDictionary = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary

            if let sequences = jsonDictionary["ListSequence"] as? [[String: AnyObject]] {
                for sequence in sequences {
                    if let name = jsonDictionary["Name"] as? String{
                        names.append(name)
                    }
                }
            }
        }
    } catch {
        ///alert
    }
}

Your JSON represents an array, not an object. Try this:

func parserJSON(data: NSData){
    var names = [String]()
    do {
        if let ipString = NSString(data:data, encoding: NSUTF8StringEncoding) {
            let json = try NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers) as! [AnyObject]
            names = json.map { ($0 as! [String: AnyObject])["Name"] as! String }
        }
    } catch {
        ///alert
        print(error)
    }
}

NSJSONSerialization is really verbose. Everything is an atomic value (ie String ), an array ( [AnyObject] ) or a dictionary ( [String: AnyObject] ). if you want to convert, you have to do it one step at a time.

Change your code like this. From API you are getting Array not Dictionary . So do changes like this

func parserJSON(data: NSData){
    var names = [String]()
    do {
        if let ipString = NSString(data:data, encoding: NSUTF8StringEncoding) {
             let jsonArray = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as! NSArray
             //Now try to iterate Array

        }
    } catch {
        ///alert
    }
}

Hope this will help you.

将数据用作NSArray

let jsonData:NSArray = NSJSONSerialization.JSONObjectWithData(urlData!, options:NSJSONReadingOptions.MutableContainers , error: &error) as NSArray

This might be easiest for you:

extension NSData {
    func fromJSON(options:NSJSONReadingOptions = .AllowFragments) -> AnyObject? {
        return (try? (NSJSONSerialization.JSONObjectWithData(self, options: options)))
    }
}

That way you could just do:

data.fromJSON() as? [[String:AnyObject]]

Or whatever you type you want to cast to

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM