简体   繁体   中英

Parsing multiple arrays from a JSON object in Swift

I am building an iOS app using Swift3 and for part of the app, I send an HTTP POST request to a webpage which returns a JSON object containing 5 different arrays. I wish to receive this JSON object in Swift and have these arrays in a readable format (NSArray). Below is exactly what my webpage returns.

{"className":["U.S. History 2 (AP)","Chemistry (HN)","Algebra 2 (HN)","Spanish 3 (HN)"],"teacherLastName":["Schartner","Racz","Johnson","Burdette"],"teacherFirstName":["Lindsey","Gregory","Shane","Joy"],"teacherTitle":["Mrs.","Mr.","Mr.","Sra."],"classID":["0001","0002","0003","0004"]}

I am attempting to do the following in my Swift code. I am not entirely sure where to go from here, but this is what I have so far.

func getClassList() -> NSArray{
    let myUrl = URL(string: "http://papili.us/studycentral/api/getClassList.php");
    var request = URLRequest(url:myUrl!)
    request.httpMethod = "POST"// Compose a query string
    let postString = "";
    request.httpBody = postString.data(using: String.Encoding.utf8);
    let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
        if error != nil {
            print("error=\(error)")
            return
        }

        // Print out response object
        print("response = \(response)")

        //Convert response sent from a server side script to a NSDictionary object:
        do {
            let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
            if let parseJSON = json {
                // Access value of username, name, and email by its key
                let newdata : NSDictionary = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSDictionary
                let info : NSArray =  newdata.value(forKey: "className") as! NSArray
                self.classList = info


            }
        } catch {
            print(error)
        }
    }
    task.resume()
    return self.classList
}

Can someone explain what I need to do in order to properly read the arrays in my JSON object? Thank you very much

Kyle,

There are a number of things going on here. The biggest mistake you are making is treating an asynchronous function as a synchronous one. The closure that you pass to dataTask gets executed asynchronously meaning it will not have completed (most likely) by the time you return self.classList . To rectify that problem, your getClassList method should itself take a closure that it will call when the data task completes.

It would look something like this:

func getClassList(completion: ((NSArray?, NSError?) -> Void)?) {
    let myUrl = URL(string: "http://papili.us/studycentral/api/getClassList.php");
    var request = URLRequest(url:myUrl!)
    request.httpMethod = "POST"// Compose a query string
    let postString = "";
    request.httpBody = postString.data(using: String.Encoding.utf8);
    let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
        if error != nil {
            print("error=\(error)")
            completion?(nil, error)
            return
        }

        // Print out response object
        print("response = \(response)")

        //Convert response sent from a server side script to a NSDictionary object:
        do {
            let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
            if let parseJSON = json {
                // Access value of username, name, and email by its key
                let newdata : NSDictionary = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSDictionary
                let info : NSArray =  newdata.value(forKey: "className") as! NSArray
                completion?(info, nil)
            }
        } catch {
            print(error)
            completion?(nil, error)
        }
    }
    task.resume()
}

The way you would access this data is by calling getClassList with a closure that takes an NSArray? and NSError? and does what you want with them. This is similar to how you called dataTask .

Also, this request looks more likely to be a GET than a POST to me. Double check that you are constructing your request according to the API you are using.

There are a number of other ways I'd suggest cleaning this code up, but I think these are the main points to move you forward.

Check this Library: JSONParserSwift

You can parse your JSON easily.

Just create following model:

class BaseModel: ParsableModel {
  var className: [String]?
  var teacherLastName: [String]?
  var teacherFirstName: [String]?
  var teacherTitle: [String]?
  var classID: [String]?
}

Now call following method:

do {
  let parsedModel: BaseModel = try JSONParserSwift.parse(string: jsonString)
} catch {
  print(error)
}

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