How to parse the specific string in a set of data?

Eekhay 101 Reputation points
2022-07-18T03:51:57.883+00:00

Hi all,

Below is the data received from the device that connect to two temperature sensors. I have tried but unable to parse the two temperatures (temperature in BOLD) to text boxes.
How to parse the temperature that in the "data":[ ] to text boxes in VB.Net. The number after the "num_sensors": showed number of temperature sensors active like the sample data below showed two active temperature sensors and the temperature in the "data":[ ] .

Main State: Idle --> read_temperatures
Read Sensors State: Not running --> start
Sensor chain initialized
Sensor Conversion Started
Read Sensors State: start --> continue
Address Temperature (deg C)
4247B561000000CF 25.56
Read Sensors State: continue --> not running
{"num_sensors":2,"addresses":[14915921967490352962,14915921967490352988],"data":[25.5625,25.78]}
Main State: Read_Temp --> Idle

Thank you.

Developer technologies | VB
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.6K Reputation points
    2022-07-18T07:48:35.767+00:00

    It is not clear what variables do you have. If there is a separate line with "{...}" data, then try something like this:

    Dim example = "{""num_sensors"":2,""addresses"":[14915921967490352962,14915921967490352988],""data"":[25.5625,25.78]}"  
      
    Dim d As MyData = System.Text.Json.JsonSerializer.Deserialize(Of MyData)(example)  
      
    Dim firstValue As Double = d.data(0)  
    Dim secondValue As Double = d.data(1)  
    
    textbox1.Text = firstValue.ToString( )  
    textbox2.Text = secondValue.ToString( )  
    

    And add a class:

    Class MyData  
        Public Property data As Double()  
    End Class  
    

    Also use the "Manage NuGet Packages" command from Project menu to add a reference to System.Text.Json (if not present yet).

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.