util.JSONObject.put
Sets a name-value pair in the JSON object.
Syntax
util.JSONObject.put(
name STRING,
value value-type )
- name is a string defining the entry name.
- value is the value to be associated to the name.
- value-type can be a primitive type, a
RECORD
,DYNAMIC ARRAY
, autil.JSONObject
orutil.JSONArray
.
Usage
The put()
method adds a name-value pair to the JSON object.
The first parameter is the name of the element.
The second parameter can be a primitive typed value such as a
STRING
or DECIMAL
, a complex variable defined as
RECORD
or DYNAMIC ARRAY
, a util.JSONObject
or a
util.JSONArray
.
If the element exists, the existing value is replaced.
Example
IMPORT util
MAIN
DEFINE obj util.JSONObject
DEFINE rec RECORD
id INTEGER,
name STRING
END RECORD
DEFINE arr DYNAMIC ARRAY OF INTEGER
LET obj = util.JSONObject.create()
CALL obj.put("simple", 234)
LET rec.id = 234
LET rec.name = "Barton"
CALL obj.put("record", rec)
LET arr[1] = 234
LET arr[2] = 2837
CALL obj.put("array", arr)
DISPLAY obj.toString()
END MAIN
Output:
{"simple":234,"record":{"id":234,"name":"Barton"},"array":[234,2837]}