How to construct Parse Objects from JSONs?

41 Views Asked by At

I have JSONs like this;

{
    createdAt: a_date,
    text: "some text",
    likes: 8
}

And I want to convert them into PFObjects… here’s how I do it;

let post = PFObject(className: "Post", dictionary: jsonPost)

The thing is that after that, the PFObject doesn’t have a value at its createdAt property.

post.createdAt <== this is always nil

I did some digging and tried this, but still no success;

jsonPost["_created_at"] = ["__type": "Date", "iso": "2020-09-23T13:31:08.877Z"]

Any idea why?

1

There are 1 best solutions below

0
On

I think you might need to manually assign each param:

let postJson = {
    createdAt: a_date,
    text: "some text",
    likes: 8
};

// create the Parse Object with Class 'Post'
let post = Parse.Object.extend('Post');
// assign param
post.set('text', postJson.text)
post.set('likes',postJson.likes)
post.save().then()...
                

createdAt is automatically handled by Parse Server when you save an object. If you want to manually control it with your own timestamp, then you will need to add a field:

post.set('myCreatedAt',postJson.createdAt)