jsonArraySize returns the number of elements in the provided JSON array.
Input:
o : SYSHANDLE
A handle to a JSON array.
Returns: INT
>=0
|
- The number of elements in the array.
|
0
|
- Function is not supported.
|
-3
|
- Not a valid handle.
|
-4
|
- Not a valid JSON array.
|
Declaration:
FUNCTION jsonArraySize : INT;
VAR_INPUT
o : SYSHANDLE;
END_VAR;
Example:
FUNCTION INTERFACE dumpJSON;
VAR_INPUT
json : SYSHANDLE;
prefix : STRING;
END_VAR;
END_FUNCTION;
FUNCTION dumpArray;
VAR_INPUT
json : SYSHANDLE;
prefix : STRING;
END_VAR;
VAR
type, i, rc : INT;
tmp : SYSHANDLE;
key : STRING;
txt : STRING;
str : STRING;
d : DINT;
b : BOOL;
f : DOUBLE;
END_VAR;
FOR i := 0 TO jsonArraySize(o:=json) - 1 DO
txt := strFormat(format:=prefix + "[\1]:", v1:=i);
type := jsonGetType(o:=json, idx:= i);
CASE type OF
1,2:
DebugFmt(message:=txt);
rc := jsonGetValue(o:=json, idx:=i, value := tmp);
dumpJSON(json:=tmp, prefix := prefix);
rc := jsonFree(o:=tmp);
3:
jsonGetString(o:=json, idx := i, value :=str);
DebugFmt(message:=txt+":$""+str+"$"");
4:
jsonGetInt(o:=json, idx := i, value :=d);
DebugFmt(message:=txt+":\4", v4:=d);
5:
jsonGetFloat(o:=json, idx := i, value :=f);
DebugFmt(message:=txt+": "+doubleToStr(v:=f));
6:
jsonGetBool(o:=json, idx := i, value :=b);
IF b THEN
DebugFmt(message:=txt+": TRUE");
ELSE
DebugFmt(message:=txt+": FALSE");
END_IF;
7:
DebugFmt(message:=txt+": NULL");
END_CASE;
END_FOR;
END_FUNCTION;
FUNCTION dumpObject;
VAR_INPUT
json : SYSHANDLE;
prefix : STRING;
END_VAR;
VAR
type, i, rc : INT;
tmp : SYSHANDLE;
key : STRING;
txt : STRING;
str : STRING;
d : DINT;
b : BOOL;
f : DOUBLE;
END_VAR;
i := 0;
REPEAT
rc := jsonGetKey(o:=json, idx:= i, key:=key, type:=type);
txt := prefix + "$""+key+"$"";
IF rc = 1 THEN
CASE type OF
1,2:
DebugFmt(message:=txt);
rc := jsonGetValue(o:=json, key:=key, value := tmp);
dumpJSON(json:=tmp, prefix := prefix);
rc := jsonFree(o:=tmp);
3:
jsonGetString(o:=json, key:=key, value :=str);
DebugFmt(message:=txt+":$""+str+"$"");
4:
jsonGetInt(o:=json, key:=key, value :=d);
DebugFmt(message:=txt+":\4", v4:=d);
5:
jsonGetFloat(o:=json, key:=key, value :=f);
DebugFmt(message:=txt+": "+doubleToStr(v:=f));
6:
jsonGetBool(o:=json, key:=key, value :=b);
IF b THEN
DebugFmt(message:=txt+": TRUE");
ELSE
DebugFmt(message:=txt+": FALSE");
END_IF;
7:
DebugFmt(message:=txt+": NULL");
END_CASE;
END_IF;
i := i+1;
UNTIL rc < 0
END_REPEAT;
END_FUNCTION;
FUNCTION IMPLEMENTATION dumpJSON;
VAR
type: INT;
END_VAR;
type := jsonGetType(o:=json, idx:=-1);
IF type = 1 THEN
DebugFmt(message:=prefix+"object{");
dumpObject(json:=json, prefix:=prefix+" ");
DebugFmt(message:=prefix+"}");
END_IF;
IF type = 2 THEN
DebugFmt(message:=prefix+"array[");
dumpArray(json:=json, prefix:=prefix+" ");
DebugFmt(message:=prefix+"]");
END_IF;
END_FUNCTION;
|