在PFIFT(parsing)转换成JSON在Swift?

有没有办法将PFObject从Parse转换成JSON? 我保存为JSON,但是当我尝试加载时,我得到了[AnyObject]。 投射到JSON将无法正常工作:

class func loadPeople() -> [String : Person] { var peopleDictionary: [String : Person] = [:] let query = PFQuery(className: "userPeeps") query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in if error == nil { //this only returns the first entry, how do I get them all? if let peopleFromParse = objects?.first?.objectForKey("userPeeps") as? JSON { for name in peopleFromParse.keys { if let personJSON = peopleFromParse[name] as? JSON, let person = Person(json: personJSON) { peopleDictionary[name] = person } } } 

下面是我的保存function,它工作并保存JSON到像我想要的parsing:

 class DataManager { typealias JSON = [String: AnyObject] class func savePeople(people: [String : Person]) { var peopleDictionary = people var peopleJSON: JSON = [:] for name in peopleDictionary.keys { peopleJSON[name] = peopleDictionary[name]!.toJSON() } let userPeeps = PFObject(className: "userPeeps") userPeeps.setObject(peopleJSON, forKey: "userPeeps") userPeeps.saveInBackgroundWithBlock { (succeeded, error) -> Void in if succeeded { println("Object Uploaded") } else { println("Error: \(error) \(error!.userInfo!)") } } } 

所以答案(就像Paulw11指出的那样)是“对象”是真实数据的一种包装,所以有必要迭代数组并将每个值存储为JSON:

 var peopleDictionary: [String : Person] = [:] //1 load the dictionary of JSON for key people from Parse let query = PFQuery(className: "userPeeps") query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in if error == nil { if let unwrappedObjects = objects { for object in unwrappedObjects { if let peopleFromParse = object as? JSON { for name in peopleFromParse.keys { if let personJSON = peopleFromParse[name] as? JSON, let person = Person(json: personJSON) { peopleDictionary[name] = person } } } } }