【问题标题】:Parsing JSON Data解析 JSON 数据
【发布时间】:2016-09-18 14:13:40
【问题描述】:

我想解析这个 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
        }

【问题讨论】:

    标签: ios iphone json swift


    【解决方案1】:

    这是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"
    
    }
    

    【讨论】:

    • 感谢您的建议,那么如何才能有地址详细信息的字符串呢?
    • 答案更新:你可以在Address中添加一个属性description,并在其中返回一个可读的字符串。然后你可以在需要的时候使用该属性。这有点像关系数据库。如果您使用过核心数据,您会发现它的行为类似于“一对一关系”。
    猜你喜欢
    • 2017-08-25
    • 1970-01-01
    • 2016-11-19
    • 2018-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多