Aegis Member Licensed User Longtime User May 14, 2020 #1 Hello Basically, I've been wondering if part of the left side of an assignment can be made parametric. To explain: Lets say i have a json of properties JSON: { "widht": "100%", "color": -1454129, "txtcolor": -12864927, "xoffset": 0 ......... }, I get the json from an mqtt server, and assign those properties to an object. Now, what if i dont know beforehand which and how many the managed properties will be? Maybe just these 4, maybe 100 or n Is it possible to write code like this? B4X: MyObject.interface.X = Y Of course assuming the json doesnt contain properties unsupported by Myobject.interface. I know something similar can be done in .net, no clue about b4x thou Thanks alot for your time
Hello Basically, I've been wondering if part of the left side of an assignment can be made parametric. To explain: Lets say i have a json of properties JSON: { "widht": "100%", "color": -1454129, "txtcolor": -12864927, "xoffset": 0 ......... }, I get the json from an mqtt server, and assign those properties to an object. Now, what if i dont know beforehand which and how many the managed properties will be? Maybe just these 4, maybe 100 or n Is it possible to write code like this? B4X: MyObject.interface.X = Y Of course assuming the json doesnt contain properties unsupported by Myobject.interface. I know something similar can be done in .net, no clue about b4x thou Thanks alot for your time
Erel B4X founder Staff member Licensed User Longtime User May 14, 2020 #2 This tool will help you parse the json string: https://b4x.com:51041/json/index.html The "automatic way" is relevant when you send data between B4X applications. You can then use B4XSerializator to convert the type to bytes and vice versa. In this case you should use a Map. Upvote 0
This tool will help you parse the json string: https://b4x.com:51041/json/index.html The "automatic way" is relevant when you send data between B4X applications. You can then use B4XSerializator to convert the type to bytes and vice versa. In this case you should use a Map.
Aegis Member Licensed User Longtime User May 14, 2020 #3 Thanks for the reply, but if i understood correctly, isnt that just skipping the json middleman? 1-Subscribe to the topic 2-Wait for the json string (or serialized map) 2-Parse that string into a map (or un-serialize the map) 3-Pass such map to a sub that creates views based on the attributes, to be added in a xcustomlistview Es. for color: B4X: Dim p As B4XView = xui.CreatePanel("") Dim panelcolor As Int = m.Getdefault("color",Colors.Transparent) p.color=panelcolor ...... ....Rinse and repeat for all the properties What im looking for , is something like this: B4X: Dim p As B4XView = xui.CreatePanel("") For Each k As String In map.Keys p.<k> = map.get(k) Next ...iterating on all the map, setting properties i didnt even consider when compiling the code Upvote 0
Thanks for the reply, but if i understood correctly, isnt that just skipping the json middleman? 1-Subscribe to the topic 2-Wait for the json string (or serialized map) 2-Parse that string into a map (or un-serialize the map) 3-Pass such map to a sub that creates views based on the attributes, to be added in a xcustomlistview Es. for color: B4X: Dim p As B4XView = xui.CreatePanel("") Dim panelcolor As Int = m.Getdefault("color",Colors.Transparent) p.color=panelcolor ...... ....Rinse and repeat for all the properties What im looking for , is something like this: B4X: Dim p As B4XView = xui.CreatePanel("") For Each k As String In map.Keys p.<k> = map.get(k) Next ...iterating on all the map, setting properties i didnt even consider when compiling the code
Erel B4X founder Staff member Licensed User Longtime User May 14, 2020 #4 As none of the key names match the B4XView properties it will not work in any way. You will need to write the code that set the properties. Upvote 0
As none of the key names match the B4XView properties it will not work in any way. You will need to write the code that set the properties.