util.JSONObject.get()

 

util.JSONObject.get() returns a value that corresponds to the specified name.

 

Syntax:

 

util.JSONObject.get("name")

 

Parameters:

 

name

a name that identifies the necessary element (= the name:value pair) of the JSON object

 

Receiving variable can be of STRING, util.JSONObject, or util.JSONArray types.

 

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 name is a simple value, the method will return a string (see example #1 ).

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 (see example #2 )).

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 (see example #3 )).

 

example code #1

(with a simple value)

MAIN

  DEFINE json_obj util.JSONObject

    

    LET json_obj = util.JSONObject.create()

    CALL json_obj.put("address", "5 Brando Street")

DISPLAY json_obj.get("address")

  

  CALL fgl_getkey()

END MAIN

 

obtained results #1

 

example code #2

(with a JSON object)

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.put("object_man", man_rec)

   

    LET json_res_obj = json_obj.get("object_man")

    DISPLAY json_res_obj.toString()

   

  CALL fgl_getkey()

END MAIN

 

obtained results #2

 

example code #3

(with a JSONArray)

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.put("cust_arr", arr)

    DISPLAY json_obj.toString()

    LET json_arr = json_obj.get("cust_arr")

    DISPLAY json_arr.toString()

  CALL fgl_getkey()

END MAIN

 

obtained results #3

 

A name:value pair can be set with the util.JSONObject.put() method.

 

 

Example programs:

CVS server: client.querix.com

CVS repository: /lycia_doc_examples

User: client

Project: auxiliary_features/json

Program: util_JSONObject_get

 

Related articles:

util.JSONObject.put()