JSONDecoder, decodes a JSON object

List

Questa pagina fa parte del Manuale Programmazione IEC 61131-3. Vai all indice.

Questa funzione effettua la decodifica di un oggetto JSON estraendo la variabile definita. Eseguendo la funzione se nell’oggetto JSON definito in Object è presente la variabile definita in Name il suo valore verrà trasferito nel buffer definito in VAddress secondo il tipo definito in VType. Se la variabile è scalare Count sarà impostato a 1, se è un array occorre definire il numero degli elementi di cui è composto l’array.

Upgrade list

JSONDecoder

Il precedente FB JSONDecode_v2 è stato trasformato in funzione con il nuovo nome. La funzione è identica nel funzionamento al FB, stesso ordine di variabili in ingresso. Il precedente valore ECode diventa il valore di ritorno.

// La precedente dichiarazione:

    JSONObject:='{"BValue":true}';
    JDecode(Object:=ADR(JSONObject), Name:=ADR('BValue'), VType:=BOOL_TYPE, VAddress:=ADR(BValue), Count:=1, Sise:=0);

// Diventa:

    JSONObject:='{"BValue":true}';
    i:=JSONDecoder(ADR(JSONObject), ADR('BValue'), BOOL_TYPE, ADR(BValue), 1, 0);
Information Circle

Funzione

CODESYS: Non disponibile

LogicLab: eLLabHTTPLib

Descrizione

Object (@STRING) Indirizzo stringa definizione oggetto JSON. Nota: L’oggetto deve essere una stringa JSON che inizia con { e termina con }.
Name (@STRING) Definizione nome variabile di cui estrarre il valore dall’oggetto.
VType (VR_TYPE) Tipo variabile di cui estrarre valore (Vedi definizione).
VAddress (@BYTE) Indirizzo appoggio valore estratto.
Count (UDINT) Se variabile scalare impostare 1. Se variabile array definire numero elementi.
Size (UDINT) Definizione dimensione buffer di stringa valore variabile. Definire 0 se la variabile non è di tipo stringa.

Immagine Fct JSONDecoder

La funzione ritorna un USINT:
0: Nessun errore.
3: Nome variabile cercata troppo lungo.
4: Variabile non presente in oggetto JSON.
5: Variabile presente più volte in oggetto JSON.
10…60: Errore in decodifica valore variabile.

Esempi

Come utilizzare gli esempi.
Sono riportati alcuni esempi di decodifica oggetti JSON, viene estratto il valore di diversi tipi di variabili.

LogicLab (Ptp156, ST_JSONDecoder)
PROGRAM ST_JSONDecoder
VAR
    i : UDINT; (* Auxiliary variable *)
    BValue : BOOL; (* Boolean value *)
    SVariable : UDINT; (* Scalar variable *)
    RVariable : REAL; (* Real variable *)
    JSONObject : STRING[ 64 ]; (* JSON object *)
    AVariable : ARRAY[0..2] OF UINT; (* Array variable *)
    SSValue : STRING[ 16 ]; (* Scalar string value *)
    AString : ARRAY[0..2] OF @STRING; (* Array string *)
    A1String : STRING[ 8 ]; (* Scalar string value [1] *)
    A2String : STRING[ 8 ]; (* Scalar string value [2] *)
    A3String : STRING[ 8 ]; (* Scalar string value [3] *)
    ENr : ARRAY[0..5] OF USINT; (* Error number *)
END_VAR

// ****************************************************************************
// PROGRAM "ST_JSONDecoder"
// ****************************************************************************
// Some JSON object decode examples.
// ----------------------------------------------------------------------------

    // ------------------------------------------------------------------------
    // JSON OBJECT DECODE
    // ------------------------------------------------------------------------
    // Decode a boolean JSON object.

    JSONObject:='{"BValue":true}';
    ENr[0]:=JSONDecoder(ADR(JSONObject), ADR('BValue'), BOOL_TYPE, ADR(BValue), 1, 0);

    // Decode a numeric JSON object.

    JSONObject:='{"SVariable":123}';
    ENr[1]:=JSONDecoder(ADR(JSONObject), ADR('SVariable'), UDINT_TYPE, ADR(SVariable), 1, 0);

    // Decode a real JSON object.

    JSONObject:='{"RVariable":12.122}';
    ENr[2]:=JSONDecoder(ADR(JSONObject), ADR('RVariable'), REAL_TYPE, ADR(RVariable), 1, 0);

    // Decode a string JSON object.

    JSONObject:='{"SString":"Hello [!]"}';
    ENr[3]:=JSONDecoder(ADR(JSONObject), ADR('SString'), STRING_TYPE, ADR(SSValue), 1, SIZEOF(SSValue));

    // Decode a numeric array JSON object.

    JSONObject:='{"AVariable":[123, 456, 789]}';
    ENr[4]:=JSONDecoder(ADR(JSONObject), ADR('AVariable'), UINT_TYPE, ADR(AVariable), 3, 0);

    // Decode a string array JSON object.

    AString[0]:=ADR(A1String); // Address of 1st array value
    AString[1]:=ADR(A2String); // Address of 2nd array value
    AString[2]:=ADR(A3String); // Address of 3rd array value
    JSONObject:='{"AString":["Is One", "Is [2]", "Three"]}';
    ENr[5]:=JSONDecoder(ADR(JSONObject), ADR('AString'), STRING_TYPE, ADR(AString), 3, 8);

// [End of file]
Was this article helpful?