Read JSON file to CODESYS Structs
Uses the PRO JSON lib by TVM, version 1.0.0.16
Some customization of FB required:
For each JSON object you should provide two types of structs:
- Your normal DUT (YOUR_STRUCT, YOUR_SUB_STRUCT in example)
- JSONVAR DUTs (note! names should match JSON file exactly) (JS_YOUR_STRUCT, JS_YOUR_SUB_STRUCT in example)
That should be done for every JSON object type. Core:
And inner:
And converting methods
- For top-most JSON object (CORE_JS_TO_ST in example)
- And for inner JSON objects (SUB_JS_TO_ST in example)
Also you should specify your DUTs in function block and maximum possible array size of the topmost object (see Limitations/Arrays )
Well, it's memory-consuming. The memory usage is specified by changing constants at GPL_JSON list in PRO_JSON library section.
The names in JSON and in ST-based JSONVARs should match exactly. For JSON fields of
{"field":"value",
"otherField":3.14
}ST-struct should be:
TYPE JSONST
STRUCT
field : JSONVAR;
otherField: JSONVAR;
END_STRUCT
END_TYPE
The Array could not be topmost structure:
[
{"obj": "value"},
{"obj": "value"}
]This is NOT valid.
The topmost array should be named:
{"myArr":
[
{"obj": "value"},
{"obj": "value"}
]}The corresponging ST structure is
TYPE JSONST
STRUCT
obj : JSONVAR;
END_STRUCT
END_TYPEAnd the name of the array in the ST code should match the JSON exactly:
VAR
myArr: ARRAY[1..10] OF JSONST;
END_VARDo not work with non-english UTF8 symbols (or I failed to do this).
Application name should match to specified in GPL_JSON list in PRO_JSON library section ('Application' by default)
For Cyryllic symols use Win-1251 (CP1251) encoding. Use Owen String Utils to deal with CP1251->WStrings
Use iconv from Linux to convert file encoding if needed.


