2016-07-15 6 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]"} 

] 
} 

私はこのjsonをSwiftyJSONで解析しようとしていますが、動作していません。このjsonのサンプルをSwiftyJSONで解析するには?

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を出力します。

json["saved_users_list"].count == 0 

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

json["title"] == null 

は、ここで私は、データを得た方法です。

+0

これまでのコードを共有できますか? (例えば、JSONの構文解析はどうですか?そして、あなたがここで共有しているもののように見えますか?) – smarx

+0

ああ、私は文字列をjsonに正しく変換していないかもしれません。 – walton

答えて

2

あなたはこの

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

ようJSON(value)を入れて試すことができますが、それはこのクラスは、それがSwiftyJSONAcceleratorモデルジェネレータを使用して生成されますあなたの問題を解決し

+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に役立ちます願っています。

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) 

} 

} 
関連する問題