Examples |
A$ = "{\22location\22:\22kitchen\22, \22temperature\22:20.5}" '\22 is the hex ASCII code for double-quote to denote strings.
B$ = GET_JSONVAL$(A$, "location")
C$ = GET_JSONVAL$(A$, "temperature")
|
Comments: |
Result from the above statements:
A$ content: {"location": "kitchen", "temperature": 20.5 }
B$ content: "kitchen"
C$ content: "20.5"
Note:
- GET_JSONVAL$ always returns an ASCII string regardless of the data type in
the "value" section of the key-value pair. However, if the value is already a string (such as "kitchen" in the
above example), the open and close double-quote character will be stripped. Hence B$ will contains just "kitchen" instead of
""kitchen"".
-
If the value is a number it will be returned as an ascii string representation of the number itself. You can then
use TBASIC VAL() or HEXVAL() functions to convert the returned string to actual numeric value.
-
The "value" itself could also be another JSON object such as {"key1",value1, "key2", value2...}, if so,
the GET_JSONVAL$ will simply return the entire json object as a string.
E.g. applying GET_JSONVAL$ function on a JSON string : {"ID": "Wx100-1", "Data":{"location": "kitchen", "temperature": 20.5 }
for the key$ = "Data" will return the entire object: {"location": "kitchen", "temperature": 20.5 } as a TBASIC string.
You can apply the GET_JSONVAL$ function on the returned string again to retrieve the key-value pairs embedded within the
new JSON object in order to retrieve the value from a multi-level JSON object.
-
If the json$ does not contain a valid JSON-formatted object this function
will return a string: "ERR: JSON object not found". If there is no key-value pair with key matches the
key$ this function will return a string
"ERR: JSON key not found" . You
can check if the first 3 characters of the returned string contains "ERR" to determine if there is an error
and take the necessary action.
-
This function is only available on Wx100 with firmware >= F94.2. Also the maximum string length for this PLC firmware
version has been increased to 232, allowing it to handle larger JSON object string.
|