util.JSONObject.get() returns a value that corresponds to the specified key.
Syntax
CALL util.JSONObject.get("key")
Parameters
key |
a key that identifies the necessary element of the JSON object (= the key:value pair) |
Usage and examples
util.JSONObject.get() returns a value, a JSON object or an element of the JSON object that corresponds to the name passed as the method's parameter.
If the element identified by the key is a simple value, the method will return a string:
MAIN
DEFINE json_obj util.JSONObject
LET json_obj=util.JSONObject.create()
CALL json_obj.insert("address","5
Brando Street")
DISPLAY json_obj.get("address")
CALL fgl_getkey()
END MAIN
If the element is structured or a part of the structured data, the method will return a util.JSONObject instance (thus, the returned instance must be assigned to a variable of the util.JSONObject type:
MAIN
DEFINE json_obj,json_res_obj util.JSONObject
DEFINE cust_rec RECORD
cust_num INTEGER,
cust_name VARCHAR(30),
order_ids DYNAMIC ARRAY OF INTEGER
END RECORD
DEFINE man_rec RECORD
man_num INTEGER,
man_name VARCHAR(30)
END RECORD
LET json_obj=util.JSONObject.create()
LET cust_rec.cust_num=345
LET cust_rec.cust_name="McMaclum"
LET cust_rec.order_ids[1] =4732
LET cust_rec.order_ids[2] =9834
LET cust_rec.order_ids[3] =2194
CALL json_obj.put("Object_cust",cust_rec)
LET man_rec.man_num=123
LET
man_rec.man_name="Black"
CALL json_obj.insert("object_man",man_rec)
LET json_res_obj=json_obj.get("object_man")
DISPLAY json_res_obj.toString()
CALL fgl_getkey()
END MAIN
If the element is a list of values or a value from this list, the method will return a util.JSONArray instance (thus, the returned instance must be assigned to a variable of the util.JSONArray type:
MAIN
DEFINE json_obj util.JSONObject
DEFINE arr DYNAMIC ARRAY OF INTEGER
DEFINE json_arr util.JSONArray
LET json_obj=util.JSONObject.create()
LET arr[1] =234
LET arr[2] =2837
CALL json_obj.insert("cust_arr",arr)
DISPLAY json_obj.toString()
LET json_arr=json_obj.get("cust_arr")
DISPLAY json_arr.toString()
CALL fgl_getkey()
END MAIN
A key:value pair can be set via util.JSONObject.put().