JSON convert form String [ST]

damian177
2021-07-01
2022-06-14
  • damian177 - 2021-07-01

    Hi, I would like to convert my STRING data exampleData (writed according with JSON format) to JSON format. I would like to get easly access form Codesys to data in JSON format ... What I done wrong ?
    Please find in attachement my example.

    Regards

     
  • i-campbell

    i-campbell - 2021-07-01

    json_p2: you used a STRING when you needed a WSTRING.
    json_p1: 'single quotes' is a STRING but you need "double quotes" for a wstring. to have a wstring literal with double quotes inside it, you need to use "double quotes with $"dollar sign double quote$" "

     
  • damian177 - 2021-07-02

    I did as you said. Please find in atatchement.
    But I still do not know how get variables form JSON to my program in ST ? Can you give me a hints ?

     
  • damian177 - 2021-07-03

    It works:

        //JSON
        // JSON data container
        jsonDataFactory : JSON.JSONDataFactory; // Factory to create a JSONData function block. 
        pJSONData : POINTER TO JSON.JSONData := jsonDataFactory.Create(); // Result of Create is a pointer to JSONData
    
        // Reader
        JSONByteArrayReader_0: JSON.JSONByteArrayReader;    
        xRead : BOOL;
        xFindValue : BOOL;
        exampleData : WSTRING(1024) := "{$"dev$": $"HOUSE-RR$", $"id$": 66,$"com$": $"OPEN DOOR$",$"val$":9,$"uid$": $"6655334455$"}";
        wsKey : WSTRING := "id";
        FindFirstValueByKey_0: JSON.FindFirstValueByKey;
        wsValue: WSTRING(JSON.GParams.g_diMaxStringSize);
    
    JSONByteArrayReader_0(xExecute:=xRead, pwData := ADR(exampleData), jsonData:= pJSONData^);
    
    FindFirstValueByKey_0(xExecute:=xFindValue, wsKey:=wsKey, diStartIndex:=0, jsonData:=pJSONData^);
    JSON.JSONElementToString(wsResult:=wsValue, element:=FindFirstValueByKey_0.jsonElement);
    

    I got result = "66" .

    Thanks for help.

     
    • atharva-joshi - 2022-06-14

      Thanks For this But I want to ask that I have 10 number(Temp, Flow, Pressure etc) Sensor data (Variable) How can i assign it in ExampleData and I want to Execute all data at same time ,
      Can you please advised

       
  • atharva-joshi - 2022-06-14

    Thanks For this But I want to ask that I have 10 number(Temp, Flow, Pressure etc) Sensor data (Variable) How can i assign it in ExampleData and I want to Execute all data at same time ,
    Can you please advised

     

Log in to post a comment.