question

karlanton avatar image
karlanton asked

Get values from Objects entity JSON?

I've got some simple data in objects entities that looks like this:

{"Couch":"45:45:90","Lamp":"1:3:45"}

Then in unity i want to get that data and store it in a list like

class Furniture
{
    string Name;
    string XandYPos;
    string YRot;
}

Using strings for simplicty now.
so i followed along with this:

https://api.playfab.com/docs/tutorials/entities/getting-started-entities

But then got confused at the bottom when they started to use different code than in the examples previously.

Anyway, I've got this

Dictionary<string, ObjectResult> _entityFileJson = new Dictionary<string, ObjectResult>();

var getRequest = new GetObjectsRequest { Entity = new EntityKey {
 Id = entityId, Type = entityType } };


        PlayFabDataAPI.GetObjects(getRequest,
        result => { _entityFileJson = result.Objects; },
        OnPlayFabError
        );

Which works and then if i use this to print it out:

            foreach(var each in _entityFileJson)
            {
                print(each.Value.DataObject.ToString());
            }

i get this :

{"Couch":"50:50:100","Lamp":"1:3:45"}

But how do i go from here? i guess its called deserialization but i dont know how.


entities
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

karlanton avatar image
karlanton answered

Found the answer by myself, if anyone has the same problem here is the solution:

    void GetFurnitureData()
    {
        
        var getRequest = new GetObjectsRequest { Entity = new EntityKey { Id = entityId, Type = entityType } };
        PlayFabDataAPI.GetObjects(getRequest,
            result => {

                JsonObject jsonResult = (JsonObject)result.Objects["Furniture"].DataObject;     
                foreach(var keys in jsonResult.Keys)
                {
                    object furniture;
                    jsonResult.TryGetValue(keys, out furniture);
                    Furnitures.Add(keys, furniture.ToString());
                }

                print("Finished retrieving data");

            },
            OnPlayFabError
        );
    }
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

kehmesis avatar image
kehmesis answered

You're a lifesaver, thanks for sharing your solution!

I'd been struggling with GetObjects().

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.