ios - 在 Swift 中将 PFObject (Parse) 转换为 JSON?

标签 ios swift parse-platform

有没有办法将 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
                    }
                }
            }

下面是我的保存函数,它可以像我想要的那样将 JSON 保存到 Parse 中:

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

关于ios - 在 Swift 中将 PFObject (Parse) 转换为 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31302330/

相关文章:

ios - RestKit @root @parent 映射访问器

swift - 在 RealityKit 场景上应用着色器

ios - 在 View 不在窗口层次结构中的 PFLogInViewController 上显示 *.viewController

带有 ContentProvider 和 BaaS 的 Android SQLite

ios - 在 Swift 中使用 PFQueryTableViewController 检查互联网连接

ios - GeoFire 给 CocoaPods 带来问题

ios - 如何在数据库中存储数据(sqlite)

ios - swift2.0,从 REST 调用返回信息(Alamofire)

ios - 在方向改变之前执行一个函数

swift - 从 PFGeoPoint 中获取纬度/经度