2016-07-15 92 views
0
{"title" : "saved users", "saved_users_list" : 

[ 
{"username" : "Danny7", "name" : "Danny", "email" : "[email protected]"}, 

{"username" : "Ike2016", "name" : "Ike", "email" : "[email protected]"}, 

{"username" : "john202", "name" : "John", "email" : "[email protected]"}, 

{"username" : "ray_mundo", "name" : "Raymundo", "email" : "[email protected]"} 

] 
} 

我想解析這個與SwiftyJSON的JSON,它不工作。如何用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) 
       } 
     } 

打印(值)打印上面列出的JSON。

+0

您可以分享您的代碼到目前爲止嗎? (例如,你如何解析JSON?你怎麼知道它看起來像你在這裏共享的?) – smarx

+0

哦,是的,它可能是我沒有正確地將字符串轉換爲json – walton

回答

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) 
     } 
    } 

希望它可以幫助

+0

你說得對。當它需要成爲.POST時,我出於某種原因意外地執行了.GET。我在做一個.GET的時候實際上得到了一個空的json對象。謝謝!!! – walton

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) 
     } 
    } 
} 
+0

價值將是一個字符串然後,我無法用SwfityJSON解析字符串 – walton

+0

@walton哦,對不起,如果你想Alamofire爲你解析JSON,需要使用'responseJSON'。看看我的編輯,雖然你的代碼應該做幾乎相同的事情。如果沒有看到您正在解析的確切字符串,或者無法通過了解URL和參數進行重現,將很難進行調試。 – smarx

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) 

} 

}