ios - 如何以 JSON 格式返回 Glassdoor API

标签 ios json swift alamofire

我在返回此处找到的 Glassdoor API 时遇到一些困难 https://www.glassdoor.com/developer/companiesApiActions.htm

如何以 JSON 格式返回整个公司,以便我可以显示公司信息以及 ceo 信息。

到目前为止,我可以显示公司信息,但无法访问 Ceo 数据和 featureReview 数据。

这就是我发出请求的方式,但不会完全以 JSON 形式返回结果。

Alamofire.request(glassdoorURL).responseJSON { (response) in
print("================")

//if let JSON = response.result.value as? [String:AnyObject] { 
//dictionary
    //print("JSON: \(JSON)")
    //let employers = JSON["response"]!["employers"]!! as [[String:AnyObject]]

if let JSON = response.result.value as? [String:Any] { //dictionary

if let resp = JSON["response"] as?[String:Any] {  //dictionary

let employers = resp["employers"] as?[[String:Any]] //array

    print("Employers: \(employers!)")

     self.companies = self.filterCompanies(companyArray: employers!)  
  //takes the company and puts it into an array to display to a table

     self.researchTableView.reloadData()
 }
//}
}
print("================")
}

这是我的数据模型。

final class GlassdoorCompany: NSObject {

var name: String? 
var websiteURL: String?
var industry: String?
var logo: String?
var overallRating: String?
var ceo: Ceo?
var featuredReview: FeaturedReview?

init(fromJSON json: NSDictionary) {
    if let nameStr = json["name"] as? String {
        self.name = nameStr
    }
    if let websiteURLStr = json["website"] as? String {
        self.websiteURL = websiteURLStr
    }
    if let industryStr = json["industry"] as? String {
        self.industry = industryStr
    }
    if let logoStr = json["squareLogo"] as? String {
        self.logo = logoStr
    }
    if let overallRatingStr = json["overallRating"] as? String {
        self.overallRating = overallRatingStr
    }
    if let ceoStr = json["ceo"] as? Ceo {
        self.ceo = ceoStr
    }
    if let featuredReviewStr = json["featuredReview"] as? 
 FeaturedReview {
        self.featuredReview = featuredReviewStr
    }
}

final class Ceo: NSObject {

var name: String?
var image: ceoImage?
var approvalRating: Int?
var disapprovalRating: Int?
var totalRatings: Int?

init?(fromJSON json: NSDictionary){

    if let nameStr = json["name"] as? String {
        self.name = nameStr
    }

    if let imageStr = json["image"] as? ceoImage {
        self.image = imageStr
    }

    if let approvalRatingStr = json["pctApprove"] as? Int {
        self.approvalRating = approvalRatingStr
    }

    if let disapprovalRatingStr = json["pctDisapprove"] as? Int {
        self.disapprovalRating = disapprovalRatingStr
    }

    if let totalRatingsStr = json["numberOfRatings"] as? Int {
        self.totalRatings = totalRatingsStr
    }
}

final class ceoImage: NSObject {

var height: Int?
var src: String?
var width: Int?

init?(fromJSON json: NSDictionary) {

    if let heightStr = json["height"] as? Int {
        self.height = heightStr
    }

    if let srcStr = json["src"] as? String {
        self.src = srcStr
    }

    if let widthStr = json["width"] as? Int {
        self.width = widthStr
    }
}

final class FeaturedReview {
var currentJob: Bool?
var reviewDate: String?
var jobTitle: String?
var location: String?
var headline: String?
var pros: String?
var cons: String?
var overallRating: Int?

init?(fromJSON json: NSDictionary) {

    if let currentJobStr = json["currentJob"] as? Bool {
        self.currentJob = currentJobStr
    }

    if let reviewDateStr = json["reviewDateTime"] as? String {
        self.reviewDate = reviewDateStr
    }

    if let jobTitleStr = json["jobTitle"] as? String {
        self.jobTitle = jobTitleStr
    }

    if let locationStr = json["location"] as? String {
        self.location = locationStr
    }

    if let headlineStr = json["headline"] as? String {
        self.headline = headlineStr
    }

    if let prosStr = json["pros"] as? String {
        self.pros = prosStr
    }

    if let consStr = json["cons"] as? String {
        self.cons = consStr
    }

    if let overallRatingStr = json["overall"] as? Int {
        self.overallRating = overallRatingStr
    }
}

最佳答案

您遇到的问题是不可能直接将 json 对象直接转换为 swift 类。那么 json["ceo"] 为? Ceojson["featuredReview"] 作为? FeaturedReview 只是返回 nil。因此,您需要更改类初始化的方式。

我已经重写了您的 CeoCeoImage 类以使其正常工作,您只需要在此基础上修复 FeaturedReview 即可。我还添加了一个 Swift 4 Codable 示例,我建议您学习使用它,因为它现在变得非常流行并且简单得多。

实现下面的新 Ceo 模型后,进行更改

if let ceoStr = json["ceo"] as? Ceo {
    self.ceo = ceoStr
}

if let ceoStr = Ceo(json["ceo"] as? Dictionary<String,Any>) {
    self.ceo = ceoStr
}

您需要解决的问题

struct Ceo {

    let name: String
    let image: CeoImage
    let approvalRating: Int
    let disapprovalRating: Int
    let totalRatings: Int

    init?(_ dict: Dictionary<String,Any>?){

        guard
            let name = dict["name"] as? String,
            let image = CeoImage(dict["image"] as? Dictionary<String,Any>),
            let approvalRating = dict["pctApprove"] as? Int,
            let disapprovalRating = dict["pctDisapprove"] as? Int,
            let totalRatings = dict["numberOfRatings"] as? Int
        else {
            return nil
        }
        self.name = name
        self.image = image
        self.approvalRating = approvalRating
        self.disapprovalRating = disapprovalRating
        self.totalRatings = totalRatings
    }
}

struct CeoImage {

    let height: Int
    let src: String
    let width: Int

    init?(_ dict: Dictionary<String,Any>?) {
        guard
            let height = dict?["height"] as? Int,
            let src = dict?["src"] as? String,
            let width = dict?["width"] as? Int
        else {
            return nil
        }
        self.height = height
        self.src = src
        self.width = width
    }
}

Swift 4 可编码示例

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        Alamofire.request(glassdoorURL).responseJSON { (response) in

            // We need to extract the `response` data from the json and convert
            // it back to `Data` for the JSONDecoder
            guard
                let dict = response.result.value as? Dictionary<String,Any>,
                let responseData = dict["response"] as? Dictionary<String,Any>,
                let json = JSONSerialization.data(withJSONObject: responseData)
            else {
                return
            }

            do {
                let dateFormatter = DateFormatter()
                dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SS"

                let decoder = JSONDecoder()
                decoder.dateDecodingStrategy = .formatted(dateFormatter)
                let company = try decoder.decode(Company.self, from: json)
                dump(company)
            } catch {
                print("\n(error)\n")
            }
        }
    }
}

型号

struct Company: Codable {

    let employers: Array<Employer>
}

struct Employer: Codable {

    let name: String
    let website: URL
    let industry: String
    let squareLogo: URL
    let overallRating: Int
    let ceo: CEO
    let featuredReview: FeaturedReview
}

struct CEO: Codable {

    struct CEOImage: Codable {
        let src: URL
        let height: Int
        let width: Int
    }

    let name: String
    let title: String
    let image: CEOImage
    let numberOfRatings: Int
    let pctApprove: Int
    let pctDisapprove: Int
}

struct FeaturedReview : Codable {

    let id: Int
    let currentJob: Bool
    let reviewDateTime: Date
    let jobTitle: String
    let location: String
    let headline: String
    let pros: String
    let cons: String
}

关于ios - 如何以 JSON 格式返回 Glassdoor API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47103909/

相关文章:

javascript - 将元素添加到空 JSON 对象 - Angular

ios - 点击更改 View 颜色,然后返回原始颜色

swift - 如何在运行时更改 Safari 应用程序扩展中的工具栏图标?

iphone - iOS 获取 subview 的旋转

iphone - UIView层次结构

ajax - 如何从 json 结果中获取这个值?

swift - 如何返回 boolean 值?

android - 使用 C++ 为 Android 和 iOS 制作 OpenGLES 2.0 库

ios - 如何获取 UICollectionView header 的索引路径?

javascript - 如何从 ASP.NET MVC 5 中的 ViewData 设置 JavaScript 变量的值