1

Scenario:

In my Parse backend, I have two classes:

A and B

There is no relation between A and B.

I have created a cloud code function called getAandB() that returns a JSON somewhat like this:

{ "anObjectOfClassA" : { 
         id = 123456,
         text = "Hello"
         },
  "anObjectOfClassB" : {
         id = 987654,
         value = 30.0
         }
}

Question:

So on the client, I use PFCloud.callFunction("getAandB") and when I receive this JSON response, is there a way to magically "transform" this into two PFObjects, one PFObject(className: "A") and one PFObject(className: "B")?

Remarks:

I know that if I only return a JSON like this:

   { "anObjectOfClassA" : { 
             id = 123456,
             text = "Hello"
             }
   }

I can "deserialize" the response and return directly a PFObject(className: "A"). But I can't do that in my example because the JSON contains two different types of objects.

Van Du Tran
  • 6,736
  • 11
  • 45
  • 55
  • Can you expand on magically "transform"? – CubanAzcuy Jul 15 '15 at 20:49
  • TL;DR: You don't need to. If you are using Parse's `PFCloud.callFunctionInBackground` for calling a cloud function from the client (iOS in my case), and if you are returning objects directly from your cloud code (return one or more objects from a query via response.success()), then the client side Parse method will automatically do this for you. I had gone to the effort of serializing the JSON, only to realize that the client side Parse functions had done this already. – Chris Conover Mar 30 '16 at 15:56
  • @chrisco even if the returning objects are of different classes? – Van Du Tran Mar 31 '16 at 14:49

1 Answers1

0

I don't know if this will work I haven't tried it but......if you receive a random JSON of Objects you wish to convert you can split the objects by key and create an array of objects you wish to be a parse object 1). Then you can split if object in the array again and get a set of properties for you parse object. Using the javascript API you can create and save JSON object for each of them. I'm confused as to why you would use that method and not just query for the information. But Some Sudo-Code Below.....

var listOfObjects = [];
for(var k in obj) listOfObjects.push(k);

for(var i =0; i < listOfObjects.length; i++){
    var parseObject = Parse.Object.extend(listOfObjects[i]);
    for(var k in listOfObjects[i]){
        parseObject.set(k, listOfObjects[i][k]);
    }
    parseObject.save();
}

1) Getting JavaScript object key list

Community
  • 1
  • 1
CubanAzcuy
  • 125
  • 1
  • 13