deserialise to list

deserialise to list

  

Hi All,

I have serialised data in the format shown below. I would like to run this though the deserialiser block to return a list that i can iterate through. The problem is i cannot figure out what datatype to use for the list. I have tried a list of records, list of strings, list of ints ect. all my attempts have returned errors. What is the correct datatype given the following serialised string?


{"0":171,"1":0,"2",5,"3":255,"4":145,"5":128,"6":0,"7":40}

Hi Anthony,

Creates a structure with 2 attributes: Text and LongInteger.
and it uses "JSONDeserialize" by passing the DataType its structure.

Regards,

Carlos Alfradique

Hi Carlos, 


Thank you for the response! is it possible to deserialize into a list instead of a structure? A list is preferred as it will allow me to iterate though it using the for each. 



Anthony Phan wrote:

Hi Carlos, 


Thank you for the response! is it possible to deserialize into a list instead of a structure? A list is preferred as it will allow me to iterate though it using the for each. 




Note I tried making a list of the structure you suggested, unfortunately I received the following error:

Failed to deserialize JSON to BLEDataList: Could not convert ]]object Object]' to List

Hi Anthony.

And if you use the xif ardoJson?

I believe it will work for your scenario.
Just be aware that your example has an error:

"2",5

Change this "," by ":"


Regards,

Carlos Alfradique

Anthony Phan wrote:

Anthony Phan wrote:

Hi Carlos, 


Thank you for the response! is it possible to deserialize into a list instead of a structure? A list is preferred as it will allow me to iterate though it using the for each. 




Note I tried making a list of the structure you suggested, unfortunately I received the following error:

Failed to deserialize JSON to BLEDataList: Could not convert ]]object Object]' to List

Hello Anthony,


With this your structure ( {"0":171,"1":0,"2",5,"3":255,"4":145,"5":128,"6":0,"7":40} ),you do not can use JSONDeserialize, you need changed your structure to, for exemple:

"[{""0"":171,""1"":0}]"


This  explain the error: JSON to BLEDataList: Could not convert ]]object Object]' to List

Have a nice Day

R.P


Solution

Hi Anthony,

The problem with your data is that it's not what you think it is. JSON has key value pairs, so your {"0":171,"1":0,"2",5,"3":255,"4":145,"5":128,"6":0,"7":40} is actually an object with seven attributes ("0" through "7"). And you can't create a list from an object.

Solution

Anthony Phan wrote:

Hi All,

I have serialised data in the format shown below. I would like to run this though the deserialiser block to return a list that i can iterate through. The problem is i cannot figure out what datatype to use for the list. I have tried a list of records, list of strings, list of ints ect. all my attempts have returned errors. What is the correct datatype given the following serialised string?


{"0":171,"1":0,"2",5,"3":255,"4":145,"5":128,"6":0,"7":40}

Hello, Anthony.

To solve your problem, you can make this:


1 )Create a structure with two attributes:

2) You can use the structure below and assing in variable type text



3) In the next step, you can use the Json2RecordList action of the ArdoJson component


4) Debug result


With this you can iterate your list quietly

If you want to modify the structure, it's easy, just follow the same pattern as Json.

I hope this helps :)

R.P

Hi Anthony,

Just what Kilian said, that means your data needs to look like:

"[{""Value"":171},{""Value"":0},{""Value"":5},{""Value"":255},{""Value"":145},{""Value"":128},{""Value"":0},{""Value"":40}]"

I've formated in a way you can copy/paste this directly into a text variable.

Also make sure that you use a list of a structure :

because you need the result in a list right. The JSONDeserialize needs to know it's a list and not just an object.

Finally, you can iterate over the list with a loop.

Before I forget, the attribute name in the structure you're gonna use needs to match exactly in you data, otherwise that entry in the final list will be left empty.

Cheers,

Menno

As an addendum to what Menno wrote, you can have the Attribute names deviate from the JSON names, by specifying an explicit "Name in JSON" property. For normal Structures, click the "JSON..." Property to edit the name in the pop-up that appears, for REST Structures, "Name in JSON" is directly available as Property.


Hi All, 

I really appreciate every bodies responses, I believe I now clearly understand that the issue lies in the way the JSON string is formatted. The string is currently constructed usingthe Stringify function in JavaScript as shown below. How can i get the string in the correct format from an array in javascript?

Note the information is being received froma bluetoothLE device.

var onData = function(buffer) {
    // Decode the ArrayBuffer into a typed Array based on the data you expect
    var data = new Uint8Array(buffer);
    var allData = JSON.stringify(data); // attempt to populate a text list directly in JavaScript

    $actions.ValueHasChanged(allData);

    $resolve();
};

ble.startNotification($parameters.DeviceId, $parameters.ServiceId, $parameters.CharacteristicId, onData, onError);

Well, the problem is that in JavaScript, an array is a kind of object, and the array "indexes" are just numerical property names, hence the output of stringify.

As for your question, that's imho outside the scope of this forum, since it seems to me that's purely a JavaScript question.