{"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。
3 个解决方案
#1
2
You can try put JSON(value)
like this
您可以尝试像这样放置JSON(值)
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
希望能帮助到你
#2
0
Doesn't Alamofire generally try to parse JSON for you? See if your code works better if you use responseJSON
and don't call JSON(...)
yourself. I.e.:
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)
}
}
}
#3
0
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)
}
}
#1
2
You can try put JSON(value)
like this
您可以尝试像这样放置JSON(值)
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
希望能帮助到你
#2
0
Doesn't Alamofire generally try to parse JSON for you? See if your code works better if you use responseJSON
and don't call JSON(...)
yourself. I.e.:
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)
}
}
}
#3
0
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)
}
}