简体   繁体   English

如何使用SwiftyJSON解析此json示例?

[英]How to parse this json example with SwiftyJSON?

{"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"}

]
}

I'm trying to parse this json with SwiftyJSON and it isn't working. 我正在尝试使用SwiftyJSON解析此json,但无法正常工作。

json["saved_users_list"].count == 0

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

json["title"] == null

Here's how I got the data: 这是我获取数据的方式:

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) prints the json listed above. print(value)打印上面列出的json。

You can try put JSON(value) like this 您可以尝试像这样放置JSON(value)

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

Hope it helps 希望能帮助到你

Doesn't Alamofire generally try to parse JSON for you? Alamofire通常不会尝试为您解析JSON吗? See if your code works better if you use responseJSON and don't call JSON(...) yourself. 如果您使用responseJSON而不自己调用JSON(...)请查看代码是否更好地工作。 Ie: 即:

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

aHope this class solves your issue, it's generated using SwiftyJSONAccelerator model generator. 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)

}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM