ios - 解析 JSON 数据

标签 ios iphone json swift

我想解析这个 JSON:http://jsonplaceholder.typicode.com/users 我在查找 JSON 结构时遇到问题。 我正在尝试使用这种结构运行良好的 JSON,但我不确定这是不是更好的方法! 解析此 JSON 数组以发布实例的最佳方法是什么? 有我的代码:

 func profileFromJSONData(data : NSData) -> ProfileResult {


         do{
        let jsonObject : NSArray!
  = try NSJSONSerialization.JSONObjectWithData(data, options: []) as! NSArray

                for profileJSON in jsonObject {
                    if let profile = profileFromJsonObject(profileJSON as! NSDictionary) {



                        finalProfile.append(profile)
                    }
                }

                return .Success(finalProfile)
            }
            catch let error {
                return .Failure(error)
            }


        }



     func profileFromJsonObject(json: NSDictionary) -> UserProfile?{

            guard let
                id = json["id"] as? Int,
                name = json["name"] as? String,
                userName = json["username"] as? String,
                email = json["email"] as? String,
                address = json["address"] as? NSDictionary,
                phone = json["phone"] as? String,
                website = json["website"] as? String,
                company = json["company"] as? NSDictionary
                else {
                    return nil
            }
            let obj = UserProfile(id: id, name: name, userName: userName, email: email, address: address, phone: phone, website: website, company: company)

            return obj
        }

最佳答案

这是苹果在Working with JSON in Swift时的建议,

并且您可以使用 flatMap 将代码改进为一行

变化自:

for profileJSON in jsonObject {
    if let profile = profileFromJsonObject(profileJSON) {
        finalProfile.append(profile)
    }
}

到:

finalProfile += jsonObject.flatMap(profileFromJsonObject)

都是一样的

处理地址:

struct Address {
    var street: String
    var city: String
    ...
    init?(json: [String: AnyObject]){...}

}

extension Address: CustomStringConvertible {

    var description: String {

        return "street: \(street), city: \(city)"
    }
}

func profileFromJsonObject(json: [String: AnyObject]) -> UserProfile? {

    guard let
            ...
            addressJson = json["address"] as? [String: AnyObject]
            address = Address(json: addressJson),
            ...
            else {
                return nil
        }

    let obj = UserProfile(id: id, name: name, userName: userName, email: email, address: address, phone: phone, website: website, company: company)

    print(address) // output: "street: Kulas Light, city: Gwenborough"

}

关于ios - 解析 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39558559/

相关文章:

ios - SFSafariViewController 没有正确解除 (iOS 10.3)

ios - 在 Adob​​e AIR 中将特定影片剪辑写入位图

iphone - UILocalNotification 触发日期问题

ios - 用于为所有 URLRequest 添加自定义 header 字段的 URLRequest 和 URLSession 的拦截器?

iphone - 从网络加载内容的最佳方式

ios - MAC Sierra 10.12 使用 xcode 调试 View 层次结构变空,不显示 View

ios - 如何在处理 catchError 后恢复数据源

objective-c - 我无法在URL中发送带有阿拉伯字母的请求

javascript - 根据值重新排列部分 JSON

php - 如何使用来自 PHP 的 jQuery/AJAX 调用遍历 JSON 数组?