GetDouble Method (JSONDATA)
10.8A, PLBCMP GUI Only
The GetDouble method gets the numeric value for a JSON field from a JSONDATA object and returns it as a user defined formatted string. The numeric value being returned is based on the type of value for the JSON field being accessed. This method uses the following format:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Where:
label
Optional. A Program Execution Label.
object
Required. A JSONDATA object previously declared.
return
Optional. A Character Variable that returns the output JSON value as a string with the format specified by the user input format. If an error occurs while retrieving the JSON value, the Character Variable is returned as a NULL variable.
name
Required. A Character String Variable or literal that specifies a JSON field name for a JSONDATA object.
format
Required. A Character String Variable or literal that specifies the format to be used for the return string.
index1
Optional. A Numeric Variable, decimal number, or Numeric expression that defines the first index of a JSON array that is being accessed.
index2
Optional. A Numeric Variable, decimal number, or Numeric expression that defines the second index of a JSON multi-dimensioned array that is being accessed.
index3
Optional. A Numeric Variable, decimal number, or Numeric expression that defines the third index of a JSON multi-dimensioned array that is being accessed.
Flags Affected: EOS, OVER, ZERO
Notes:
The EOS flag is set if the output JSON string is truncated because the {return} variable is too small.
The ZERO and OVER flags are always cleared.
At runtime, the tokens $1, $2, and $3 that appear in the {name} parameter are replaced with the numeric values of the corresponding {index1}, {index2}, or {index3} parameters.
The {return} value that is returned as a formatted string is determined by the JSON field value type being accessed using a JSON field key specified by the {name} parameter. The JSON field value types are defined as follows:
|
Type |
Return Value |
|
$JSF_TYPE_OBJECT (1) |
Returns the number of fields in the json object. |
|
$JSF_TYPE_ARRAY (2) |
Returns the number of elements in the array. |
|
$JSF_TYPE_INTEGER (3) |
Returns the field integer value. |
|
$JSF_TYPE_DOUBLE (4) |
Returns the whole digits value. Decimal digits are not returned. |
|
$JSF_TYPE_STRING (5) |
Returns the length of the JSON field string. |
|
$JSF_TYPE_BOOLEAN (6) |
Returns the value of one for a 'true' value. If the value is 'false', the return value is zero. |
|
$JSF_TYPE_NULL (7) |
Returns the value of zero. |
The {format} string is the same as the common 'sprintf' formats used for a 'C' double value. The {format} string formats are described as follows:
|
Format |
Output Style |
Notes |
|
%f |
Fixed-point decimal |
Default is 6 digits after decimal. |
|
%.nf |
Fixed-point decimal specifying number of decimal digits |
The 'n' value specifies the number.of decimal digits. If the 'n' value is larger than 6, the output decimal digits can be indeterminate.
Example: ( %.2f ) |
|
%e or %E |
Scientific notation |
Lowercase 'e' vs uppercase 'E' for exponent. |
|
%g or %G |
Shortest representation (fixed or scientific) |
Chooses between '%f' and '%e' depending on the magnitude, trims trailing zeros. |
Here are some example 'GetDouble' outputs:
|
Format |
Value |
Output Returned |
|
%f |
12345.6789 |
12345.678900 |
|
%.2f |
12345.6789 |
12345.68 |
|
%.10f |
12345.6789 |
12345.6789000000 |
|
%e |
12345.6789 |
1.2345678e+04 |
|
%.3e |
12345.6789 |
1.235e+04 |
|
%.3E |
12345.6789 |
1.235E+04 |
|
%g |
312345.6789 |
312346 |
|
%.5g |
312345.6789 |
3.1235e+05 |
|
%.5G |
312345.6789 |
3.1235E+05 |
If an error occurs, the JSONDATA object method 'SyntaxError' canbe used to retrieve that last error that was generated.
Example:
.
oJson JSONDATA
.
cFmt CONST "9"
dFmtArr DIM 6(cFmt)
dFmt INIT "%f;%.2f;%.10f;%e;%.3e;%.3E;%g;%.5g;%.5G"
nFmtNdx FORM "0"
dString DIM 65
.
names DIM 1024
result FORM 10
nameArr DIM 25(cFmt)
.
jsonString init "{#"test1#":12345.6789,":
"#"test2#":12345.6789,":
"#"test3#":12345.6789,":
"#"test4#":12345.6789,":
"#"test5#":12345.6789,":
"#"test6#":12345.6789,":
"#"test7#":312345.6789,":
"#"test8#":312345.6789,":
"#"test9#":312345.6789}"
.
EXPLODE dFmt, ";", dFmtArr
.
oJson.Parse Giving result Using *JSONDATA=jsonString
IF ( result != 0 )
DISPLAY "Error...Do 'oJson.SyntaxError'"
STOP
ENDIF
.
oJson.GetNames Giving names Using names
.
EXPLODE names, ",", nameArr
.
FOR nFmtNdx, 1, cFmt
.
oJson.GetDouble Giving dString:
Using *Name=nameArr(nFmtNdx):
*Format=dFmtArr(nFmtNdx)
.
DISPLAY *LL, nameArr(nFmtNdx)," ==> ",dString
.
REPEAT
.
DISPLAY *W15
See Also: Method Syntax, JSONDATA Methods
![]() |