Hello.
I’ve been trying to parse the JSON data retrieved by the server with some user information.
I’ve successfully queried the server with a dataQuery with the specific whereClause, and it returned the required data in JSON format.
As it goes, I’ve been trying to parse that data from BackendlessCollection to NSData so I can use the NSJSON.
My code goes like the following:
let email: String = "test@gmail.com"
let query = BackendlessDataQuery()
query.whereClause = "email = '\(email)'"
let dataStore = Backendless().persistenceService.of(BackendlessUser.ofClass())
dataStore.find(query, response: { (collection) -> Void in
print("User information found: \(collection)")
do {
let data = collection as! NSData
let json = try NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments)
if let objectId = json["objectId"] as! String? {
print("objectId -> \(objectId)")
}
} catch {
print("CATCH ERROR")
}
}) { (fault) -> Void in
print("an error was found: \(fault)")
}
But in this case Xcode is prompting that the casting of BackendlessCollection to NSData will always fail.
How can we handle properly the returned data from the backend?
King regards,
-IC
Ivan,
When you run this code:
let dataStore = Backendless().persistenceService.of(BackendlessUser.ofClass())
dataStore.find(query, response: { (collection) -> Void in
The “collection” object you get back is an instance of BackendlessCollection which already contains objects of the type specified here: Backendless().persistenceService.of( YOUR-CLASS-TYPE )
This means in your case the data you get back are already BackendlessUser objects. You do not need to do any JSON manipulations at all - just use the objects you got back in the collection.
Hope this helps.
Mark
Ohh… How did I miss that! I miss-read the documentation.
I really thought the data was retrieved as a JSON regular doc.
I’ll do as you said.
Thanks for the reply Mark.
Cheers.
Thanks.
Now I have another doubt.
After returning the filled collection from the backend , how can I access a single instance/property of it?
The users collection returns all the default users properties of the backend, plus the ones that I’ve manually added, but I can’t figure out how to get them out.
For example, the collection is printed like this:
collection: [<BackendlessUser> {
"___class" = Users;
"__meta" = "{\"relationRemovalIds\":{},\"selectedProperties\":[\"password\",\"created\",\"name\",\"___class\",\"ownerId\",\"updated\",\"email\",\"objectId\"],\"relatedObjects\":{}}";
created = "2016-01-30 01:22:56 +0000";
email = "email@email.com";
lastLogin = "2016-02-18 22:34:30 +0000";
name = "";
objectId = "FULL-ID";
ownerId = "<null>";
password = "<null>";
updated = "<null>";
userStatus = ENABLED;
}]
How can I access the name instance , or the lastLogin?
I’ve tried the collection.valueForKey(“lastLogin”) , but I don’t know if it’s through this path
Thanks in advance
use the “getProperty” method on the BackendlessUser object.
Thanks.
Everything is solved now.
Amazing support man.
Looking forward to seeing your app!! ))