Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

JSON. transmission...

ANSWERED

Hello,

 

this an other basic question but still i have some troubles with sending an array from companion to my  app...

 

in companion i m sending an array to app as so: 

 let myData = {voila : mesMoyennes,} 
 sendVal(myData);



function sendVal(data) {
  if (messaging.peerSocket.readyState === messaging.peerSocket.OPEN) {
    messaging.peerSocket.send(data);
  }

then in app i m receiving the message using JSON as so :

 if (evt.data.voila!=null)
      {      
// stringArray = (JSON.stringify(evt.data.voila));
// mesReves =  JSON.parse(stringArray);
   mesReves=`${JSON.stringify(evt.data.voila)}`
   console.log ("is that right?" + mesReves);
   console.log ("is that possible?" + mesReves[0].reve);

      }

what is the right way to store a message after JSON transmitting it so i can access the index[i] of the array again?

what ever i do `${JSON.stringify (evt.data.myVar)]` does display the content of the array, but it still not the array i can use. wat is the proper way to do that.

thank you for answering !

 

 

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

@Slumberlander wrote:

Hello,

 

this an other basic question but still i have some troubles with sending an array from companion to my  app...

 

...

 

what is the right way to store a message after JSON transmitting it so i can access the index[i] of the array again?

what ever i do `${JSON.stringify (evt.data.myVar)]` does display the content of the array, but it still not the array i can use. wat is the proper way to do that.

thank you for answering !

 

 


Hello @Slumberlander ,

JSON isn't really necessary in this case. You are going to receive an object (evt.data.voila). You can just assign it to a variable and use it.

 

pSok.onmessage=evt=>{
  if (ex) return
  if (kpay_common.isKPayMessage(evt.data)){console.log("kpay");return;}
  if (!timeTest) tD = new Date()
  cDate=tD.getTime()
  var eD = evt.data.dR
...

 

You can also call a routine that will update your current variables with your new message:

 

var e={/* INITIALIZE COMM OBJECT(S) HERE*/}

update = (vals) => {
  for (let x in vals) {
    if (typeof e[x] === typeof vals[x]) e[x] = vals[x]
  }
}

 

Then an "update(eD)" command will move your new comm values into a common object (in this case 'e') where it can be used as e['voila'] for example.

 

If you are using the file system, specifying a cbor or a json will take care of the stringifying and parsing for you. The only place I've needed to do this explicitly was with the settings storage API; you don't necessarily have to stringify a setting, but it works better if you do.

 

I believe the messaging system compresses to a cbor in transit, but the result is an object, perhaps a very complex object, but an object nonetheless.

 

Regards,

Reign

View best answer in original post

Best Answer
3 REPLIES 3

 

 

stringArray = JSON.stringify(evt.data.voila);
mesReves = JSON.parse(stringArray);

Best Answer
0 Votes

@Slumberlander wrote:

Hello,

 

this an other basic question but still i have some troubles with sending an array from companion to my  app...

 

...

 

what is the right way to store a message after JSON transmitting it so i can access the index[i] of the array again?

what ever i do `${JSON.stringify (evt.data.myVar)]` does display the content of the array, but it still not the array i can use. wat is the proper way to do that.

thank you for answering !

 

 


Hello @Slumberlander ,

JSON isn't really necessary in this case. You are going to receive an object (evt.data.voila). You can just assign it to a variable and use it.

 

pSok.onmessage=evt=>{
  if (ex) return
  if (kpay_common.isKPayMessage(evt.data)){console.log("kpay");return;}
  if (!timeTest) tD = new Date()
  cDate=tD.getTime()
  var eD = evt.data.dR
...

 

You can also call a routine that will update your current variables with your new message:

 

var e={/* INITIALIZE COMM OBJECT(S) HERE*/}

update = (vals) => {
  for (let x in vals) {
    if (typeof e[x] === typeof vals[x]) e[x] = vals[x]
  }
}

 

Then an "update(eD)" command will move your new comm values into a common object (in this case 'e') where it can be used as e['voila'] for example.

 

If you are using the file system, specifying a cbor or a json will take care of the stringifying and parsing for you. The only place I've needed to do this explicitly was with the settings storage API; you don't necessarily have to stringify a setting, but it works better if you do.

 

I believe the messaging system compresses to a cbor in transit, but the result is an object, perhaps a very complex object, but an object nonetheless.

 

Regards,

Reign

Best Answer

thank you reign ! 

 

Best Answer
0 Votes