jsonFromString (Function)

Top  Previous  Next

Architecture:

NX32L

Device support:

All

Firmware version:

2.10.00


jsonFromString creates a new JSON structure by parsing the given string.

 

When the JSON structure is no longer needed, it must be released using jsonFree.

 

 

When needing to create large JSON structures with mostly fixed data, one option is to use strTemplateCreate to add the dynamic data to a string with custom JSON data, which can then be used as input to jsonFromString.

 

 

Input:

str : STRING

JSON string to create the structure from.

 

Output:

o : SYSHANDLE

A handle to the new JSON structure.

 

Returns: INT

1

- Success

0

- Function is not supported.

-6

- Could not create JSON structure, there may be too many in use.

-99

- Failed to create JSON structure, string might not be valid JSON.

 

 

Declaration:

FUNCTION jsonFromString : INT;
VAR_INPUT
  str   : STRING;
  o     : ACCESS SYSHANDLE;
END_VAR;

 

 

Example:

INCLUDE rtcu.inc
 
PROGRAM test;
VAR
  rc  : INT;
  obj : SYSHANDLE;
  str : STRING;
END_VAR;
 
BEGIN
  str := STRING_BEGIN
{
  "glossary":{
     "title":"example glossary",
     "GlossDiv":{
        "title":"S",
        "GlossList":{
           "GlossEntry":{
              "ID":"SGML",
              "SortAs":"SGML",
              "GlossTerm":"Standard Generalized Markup Language",
              "Acronym":"SGML",
              "Abbrev":"ISO 8879:1986",
              "GlossDef":{
                 "para":"A meta-markup language, used to create markup languages such as DocBook.",
                 "GlossSeeAlso":[
                    "GML",
                    "XML"
                 ]
              },
              "GlossSee":"markup"
           }
        }
     }
  }
}
STRING_END;
 
  ...
  // Create structure from string
  rc := jsonFromString(o := obj, str := str);
 
  // Dump structure to device output
  rc := jsonToString(o := obj, indent:=3, str := str);
  DebugMsg(message:=str);
  ...
END;
 
END_PROGRAM;