C
C#9mo ago
Saya

Reading nested dictionaries received via MQTT

Hi, so basically someone coded a script, which sends JSONs per MQTT. I want to receive them in Unity. I managed to receive JSONS which were made out of simple key-value pairs which were sent to me by MQTT. But now I should receive a JSON which has a variable amount of nested dictionaries, and I am not sure how to work with them, and if it is even possible to deserialize them correctly. So I would be glad if someone could help me. One example written in Python is:
data = {'Line1': {'self': {'state': 'GR1', 'product_at': 'GR1'}, 'GR1': {'status': 'GRIPPING'}, 'MPS': {'status': 'INIT'}}}

{'status': 'INIT'} == data['Line1']['MPS'];
'INIT' == data['Line1']['MPS']['status'];
'GRIPPING' == data['Line1']['GR1']['status'];
{'self': {'state': 'GR1', 'product_at': 'GR1'}, 'GR1': {'status': 'GRIPPING'}, 'MPS': {'status': 'INIT'}} == data['Line1'];
data = {'Line1': {'self': {'state': 'GR1', 'product_at': 'GR1'}, 'GR1': {'status': 'GRIPPING'}, 'MPS': {'status': 'INIT'}}}

{'status': 'INIT'} == data['Line1']['MPS'];
'INIT' == data['Line1']['MPS']['status'];
'GRIPPING' == data['Line1']['GR1']['status'];
{'self': {'state': 'GR1', 'product_at': 'GR1'}, 'GR1': {'status': 'GRIPPING'}, 'MPS': {'status': 'INIT'}} == data['Line1'];
there you can see that it is a nested dictionary Line1 is a key and has several other dictionaries nested inside, which I can call by the keys "self", "GR1", "MPS" and others. data like this is sent like every few seconds, but it can always have a different structure. Like there can be additional dictionaries similar to the ones called you can call with "MPS" or "GR1". Those can be called "GR2", "CB1" for example. so, first at all I would like to ask, if it would be possible to work with this in C#, since from what I have seen so far, C# needs you to define corresponding classes for this type of problem. And since the data always variates with different amounts of dictionaries nested inside the data, I wonder how to define a class like this does anyone know if it is possible to work with data like this in unity?
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server