简体   繁体   English

在Swift中将PFObject(Parse)转换成JSON?

[英]Converting PFObject (Parse) into JSON in Swift?

Is there a way to convert a PFObject from Parse into JSON? 有没有一种方法可以将PFObject从Parse转换为JSON? I saved as JSON, but when I try to load I'm getting [AnyObject] back. 我已另存为JSON,但是当我尝试加载时,却又找回了[AnyObject]。 Casting to JSON won't work: 强制转换为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
                    }
                }
            }

below is my save function, which works and saves the JSON into Parse like I want: 以下是我的保存功能,该功能可以按需要将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!)")
                        }
                    }

    }

So the answer (as Paulw11 points out above) is that "objects" is sort of a wrapper for the real data, so it was necessary to iterate through the array and store each value as JSON: 因此,答案(如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
                                }
                            }
                        }
                    }
                }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在PFObject中管理枚举-iOS Swift / Parse - Managing an enum within a PFObject - iOS Swift/Parse iOS解析带有Swift行为的PFObject子类化 - iOS Parse PFObject Subclassing with Swift Behaviour Swift:PFObject为Array时解析PFQueryTableViewController-numberOfRowsInSection - Swift : Parse PFQueryTableViewController - numberOfRowsInSection when PFObject is Array 解析,Swift和Xcode 6.1-无法通过调用.object()创建PFObject子类 - Parse, Swift and Xcode 6.1 - cannot create PFObject subclass by calling .object() 使用Swift解析“快速启动” - “PFObject”没有名为“下标”的成员 - Parse 'Quick Start' with Swift - 'PFObject' does not have a member named 'subscript' Swift中的parse.com - 是否可以将检索到的PFObject转换为子类? - parse.com in Swift - is it possible to cast retrieved PFObject as subclass? 解析SDK和Swift:调用PFObject'withoutDataWithObjectId'中的参数标签不正确 - Parse SDK and Swift: Incorrect argument label in call PFObject 'withoutDataWithObjectId' PFObject错误没有名为“ Contains”的成员(SWIFT Xcode Parse) - Error with PFObject does not have a member named 'Contains' (SWIFT Xcode Parse) 是否可以将JSON中的对象转换为Parse.com对象(PFObject) - Is it possible to convert objects in a JSON to Parse.com objects (PFObject) 解析-PFObject-属性 - Parse - PFObject - property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM