如何用SwiftyJSON解析这个json示例?

时间:2016-07-15 00:27:22

标签: ios json swift swifty-json

{"title" : "saved users", "saved_users_list" :

[
{"username" : "Danny7", "name" : "Danny", "email" : "danny@email.com"},

{"username" : "Ike2016", "name" : "Ike", "email" : "ike@email.com"},

{"username" : "john202", "name" : "John", "email" : "john@email.com"},

{"username" : "ray_mundo", "name" : "Raymundo", "email" : "ray@email.com"}

]
}

我正在尝试用SwiftyJSON解析这个json,但它无法正常工作。

json["saved_users_list"].count == 0

json["saved_users_list"][0]["username"] does not equal "Danny7"

json["title"] == null

以下是我获取数据的方式:

Alamofire.request(.POST, url, parameters: parameters).validate().responseString { response in
            switch response.result {
            case .Success:
                if let value = response.result.value {
                    value = JSON(value)
                    print(value)
                }
        }

print(value)打印上面列出的json。

3 个答案:

答案 0 :(得分:2)

您可以尝试像这样放置JSON(value)

Alamofire.request(.POST, url, parameters: parameters).responseJSON { (response) in
        switch response.result {
        case .Success(let value) :
            let swiftyJSON = JSON(value)
            print(swiftyJSON)
        }
    }

希望有所帮助

答案 1 :(得分:0)

不是Alamofire通常会尝试为您解析JSON吗?如果您使用responseJSON并且不要自己致电JSON(...),请查看您的代码是否更有效。即:

Alamofire.request(.POST, url, parameters: parameters).validate().responseJSON { response in
    switch response.result {
    case .Success:
        if let value = response.result.value {
            print(value)
        }
    }
}

答案 2 :(得分:0)

aHope这个类解决了你的问题,它是使用SwiftyJSONAccelerator模型生成器生成的。

public class BaseClass: NSObject, NSCoding {

// MARK: Declaration for string constants to be used to decode and also serialize.
internal let kBaseClassTitleKey: String = "title"
internal let kBaseClassSavedUsersListKey: String = "saved_users_list"


// MARK: Properties
public var title: String?
public var savedUsersList: [SavedUsersList]?


// MARK: SwiftyJSON Initalizers
/**
Initates the class based on the object
- parameter object: The object of either Dictionary or Array kind that was passed.
- returns: An initalized instance of the class.
*/
convenience public init(object: AnyObject) {
    self.init(json: JSON(object))
}

/**
Initates the class based on the JSON that was passed.
- parameter json: JSON object from SwiftyJSON.
- returns: An initalized instance of the class.
*/
public init(json: JSON) {
    title = json[kBaseClassTitleKey].string
    savedUsersList = []
    if let items = json[kBaseClassSavedUsersListKey].array {
        for item in items {
            savedUsersList?.append(SavedUsersList(json: item))
        }
    } else {
        savedUsersList = nil
    }

}


/**
Generates description of the object in the form of a NSDictionary.
- returns: A Key value pair containing all valid values in the object.
*/
public func dictionaryRepresentation() -> [String : AnyObject ] {

    var dictionary: [String : AnyObject ] = [ : ]
    if title != nil {
        dictionary.updateValue(title!, forKey: kBaseClassTitleKey)
    }
    if savedUsersList?.count > 0 {
        var temp: [AnyObject] = []
        for item in savedUsersList! {
            temp.append(item.dictionaryRepresentation())
        }
        dictionary.updateValue(temp, forKey: kBaseClassSavedUsersListKey)
    }

    return dictionary
}

// MARK: NSCoding Protocol
required public init(coder aDecoder: NSCoder) {
    self.title = aDecoder.decodeObjectForKey(kBaseClassTitleKey) as? String
    self.savedUsersList = aDecoder.decodeObjectForKey(kBaseClassSavedUsersListKey) as? [SavedUsersList]

}

public func encodeWithCoder(aCoder: NSCoder) {
    aCoder.encodeObject(title, forKey: kBaseClassTitleKey)
    aCoder.encodeObject(savedUsersList, forKey: kBaseClassSavedUsersListKey)

}

}